Passing a file along with the program in Xcode 3

Discussion in 'Mac Programming' started by Psycho7, Mar 25, 2008.

  1. macrumors newbie

    Joined:
    Mar 25, 2008
    #1
    Hello,

    I've been trying to pass a file along with my program in xcode but have found no way of doing so.

    On the other hand, using terminal I am able to do so using the following syntax:

    ./program1 test.obj

    where 'program1' is my program and 'test.obj' is the file I want to pass into it

    Is there anyway I can pass the file through xcode?

    Thank you

    PS: My program is written in C and I started the project with the 'C command line' option
     
  2. macrumors regular

    Joined:
    Jan 17, 2007
    #2
    Yup, you can do this in Xcode. In the sidebar, expand Executables and right click on your program1 and go to Get Info. Then click the Arguments tab and all you need to do is click the + button and add your own. Just make sure you add them to the arguments and not the environment variables.
     
  3. TEG
    macrumors 604

    TEG

    Joined:
    Jan 21, 2002
    Location:
    Langley, Washington
    #3
    If you are trying to run in XCode, there should be a setting for command line arguments, that is where you input your test file to pass in.

    TEG
     
  4. thread starter macrumors newbie

    Joined:
    Mar 25, 2008
    #4
    Wow! That was quick! Thanks

    CaptainZap: What do you mean add your own in the argument tab? Do I need to put the file that I will pass (test.obj) in the same folder? How exactly does it work? Isn't there some way to input the file just as how I did in the Terminal?

    Thank you
     
  5. macrumors regular

    Joined:
    Jan 17, 2007
    #5
    How is your program using the argument? Is it using it as a path that then accesses the file?
     
  6. thread starter macrumors newbie

    Joined:
    Mar 25, 2008
    #6
    It's taken in like this from one of the source files in my program

    int main (int argc, char** argv)

    the file, test.obj is different.

    When I run the program using terminal, it works perfectly

    using ./program1 test.obj

    where both program1 and test.obj are in the same folder.

    But when I execute the file, program1 from Xcode, it quits as the terminal does not allow me to enter the 'test.obj'

    Right now I compile my program in xcode and transfer the executable to another folder with the test.obj and run it through the Terminal to test it.
     
  7. macrumors regular

    Joined:
    Jan 17, 2007
    #7
    I guess I'm not quite understanding what your program is doing with test.obj. Can you post the part of the code that uses the argument?

    Try putting a full path to test.obj as an argument.
     
  8. thread starter macrumors newbie

    Joined:
    Mar 25, 2008
    #8
    Code:
    /*  
     Includes a main function that reads the name of an LC-3 object file
     from the command line, then parses the file and passes the instructions
     in the file to the disassemble function for disassembly.
     */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/stat.h>
    
    
    static const int MAX_FILE_NAME_LEN = 250;
    
    
    /*Exit Conditions*/
    enum {
        EXIT_SUCCEED  = 0,
        EXIT_FAIL     = 1,
        EXIT_BAD_ARGS = 2,
        EXIT_PANIC    = 3
    };
    
    
    /* 
     The arguments to main are taken from the command line.
     The second paramater, argv, is an array of strings, with each string
     corresponding to one of the space-separated "words" on the command
     line.  The first element of this array is the command's name,
     and the others are the arguments.  The first argument, argc, is
     a count of the number of elements in the argv array.
     */
    
    int main (int argc, char** argv)
    {
        int len;                              /* length of given file name   */
        char filename[MAX_FILE_NAME_LEN + 5]; /* private copy of file name   */
        struct stat obj_stats;                /* object file statistics      */
        int num_words;                        /* number of instructions      */
        unsigned short* inst;                 /* pointer to instructions     */
        FILE* obj_file;			  /* the object file             */
        unsigned char buf[4];		  /* a buffer for reading        */
        int begin_addr;                       /* the code's starting address */
        int inst_count;                       /* count of instructions read  */
    	
        /* Check command line syntax. */
        if (argc != 2) {
            fprintf (stderr, "syntax: %s <object file>\n", argv[0]);
    		return EXIT_BAD_ARGS;
        }
    	
        /* Check length of object file name. */
        len = strlen (argv[1]);
        if (len < 1 || len > MAX_FILE_NAME_LEN) {
            fputs ("object file name too short or long\n", stderr);
    		return EXIT_BAD_ARGS;
        }
    	
        /* Make a copy of the object file name in the filename variable.
    	 If no extension appears in the file name, add .obj. */
        strcpy (filename, argv[1]);
        if (strrchr (filename, '.') == NULL)
    		strcpy (filename + len, ".obj");
    	
        /* Check how many instructions are in the object file.  The first
    	 word is the starting address, which we handle separately. */
        if (stat (filename, &obj_stats) == -1) {
    		/* Type in the name of a non-existent file to see the
    		 error message generated by this library routine. */
            perror ("Could not determine object file length");
    		return EXIT_BAD_ARGS;
        }
        num_words = (obj_stats.st_size / sizeof (*inst)) - 1;
    	
    
        if ((obj_stats.st_size % sizeof (*inst) != 0) ||
            num_words < 1 || num_words > 65536) {
    		fprintf (stderr, "%s does not seem to be an LC-3 object file.\n",
    				 filename);
            return EXIT_BAD_ARGS;
        }
    	
        /* Dynamically allocate an array of unsigned short integers to hold
    	 the instructions.  If we don't have enough memory, give up. */
        inst = malloc (num_words * sizeof (*inst));
        if (inst == NULL) {
            perror ("Dynamic allocation failed");
    		return EXIT_PANIC;
        }
    
        /* Open the object file. */
        obj_file = fopen (filename, "r");
        if (obj_file == NULL) {
            perror ("Open object file");
    		return EXIT_BAD_ARGS;
        }
    	
      
        if (fread (buf, sizeof (*inst), 1, obj_file) != 1) {
    		fputs ("Could not read all instructions.\n", stderr);
    		return EXIT_PANIC;
        }
        begin_addr = (buf[0] << 8) | buf[1];
    	
        /* Finally, read the instructions and close the file. */
        for (inst_count = 0; inst_count < num_words; inst_count++) {
    		if (fread (buf, sizeof (*inst), 1, obj_file) != 1) {
    			fputs ("Could not read all instructions.\n", stderr);
    			return EXIT_PANIC;
    		}
            inst[inst_count] = (buf[0] << 8) | buf[1];
        }
        fclose (obj_file);
    	
        /* Disassemble the instructions. 
            This function calls on the second source file which disassembles the   
            code*/
        disassemble (begin_addr, num_words, inst);
        /* We're done. */
        return EXIT_SUCCEED;
    }
    
    
     
  9. macrumors regular

    Joined:
    Jan 17, 2007
    #9
    Yah, a full path to the file should work, or else drag it into your project where the executable is and then the argument can just be the file name.
     
  10. thread starter macrumors newbie

    Joined:
    Mar 25, 2008
    #10
    Both options don't seem to work. Xcode won't let me drag the my test.obj file into executables folder nor does the full path work. Maybe i'm not completely understanding what your telling me to do. Could you give me a little more on exactly how i'm supposed to bring my test.obj file into xcode? Thank you!

    I've found another way to do but still not within Xcode, I just drag the executable into terminal and write the file name next to it which works out better than copying the file to a new destination.

    But i'm sure xcode must be able to do the same thing.

    Thank you!
     
  11. TEG
    macrumors 604

    TEG

    Joined:
    Jan 21, 2002
    Location:
    Langley, Washington
    #11
    There are panels in XCode that allow you to set Arguments. All you need to do is enter the full path to your test.obj into it, starting with '/'.

    TEG
     
  12. thread starter macrumors newbie

    Joined:
    Mar 25, 2008
    #12
    Thank you for the reply TEG but it still doesn't seem to work. I tried putting the test.obj file in the executable folder as well but the program does not read the file.

    /Users/myname/Desktop/Project 4/build/Release/test.obj

    That was what I put in the argument tab but it still does not work

    And when I placed the test.obj file in the executable folder, I just passed test.obj in the arguments tab.

    No luck with both methods
     
  13. TEG
    macrumors 604

    TEG

    Joined:
    Jan 21, 2002
    Location:
    Langley, Washington
    #13
    When I use XCode, I find it easier to compile and run outside of XCode (only using it for the Syntax Highlighting).

    TEG
     
  14. macrumors regular

    Joined:
    Jan 17, 2007
    #14
    Sorry, I couldn't reply sooner but your post below answered your question.

    But I think I see the problem. The path to your test.obj has a space in it causing your program to read only the first part of the argument. Either try changing the name, putting quotes around it, or putting in a \ before the space. I'd recommend changing the name because I'm not sure how it will handle the argument with quotes or with a \.

    Hopefully that works =D
     
  15. thread starter macrumors newbie

    Joined:
    Mar 25, 2008
    #15
    Hello again CaptainZap,

    Sorry for the late reply. I just put the test file in the executable folder like you said and wrote the test.obj as an argument and it worked!

    Thank you so much!
     
  16. macrumors regular

    Joined:
    Jan 17, 2007
    #16
    Haha no problem, I'm just glad it worked :p
     
  17. macrumors regular

    Joined:
    Jan 8, 2003
    Location:
    New York
    #17
    forgive me...

    Forgive me, I know this thread is long dead... but... I am trying to do something very similar...

    I have a (C++) program compiled with Xcode 3, and I want to be able to drop a file onto the application to launch it, and have the filepath passed as an argument to my application. When I do this, my application launches, but the file that I dropped is not there in the arguments passed to the main() argv[].

    Help?
     
  18. macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #18
    This is completely different. It's not passed as an argument to main. As far as I'm aware, it's part of the normal document-based subsystem (NSDocument), but I haven't actually had to program this part of an app myself.

    http://en.wikibooks.org/wiki/Progra...coa_for_beginners/Document-based_applications
     
  19. macrumors regular

    Joined:
    Jan 8, 2003
    Location:
    New York
    #19
    I'm building a C++ application, so the Cocoa classes are not available to me... any other ideas?
     
  20. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #20
    Is this supposed to be a cross-platform application?
     
  21. macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #21
    Carbon would be your other option, unless you're going the cross-platform route, as Eraserhead asked. If so, wxWidgets (for example) would handle all of the underlying system calls. Even if you aren't going that route, you could still use Objective-C++ through a PIMPL (private implementation).

    Regardless, if you don't know anything about the system-provided document architectures, you should probably start with an intro to Cocoa book, as that will probably be the last several chapters in whatever books are currently popular. You aren't going to get remotely enough information just asking questions at macrumors.
     

Share This Page