Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 25, 2008, 07:39 PM   #1
Psycho7
macrumors newbie
 
Join Date: Mar 2008
Passing a file along with the program in Xcode 3

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

Last edited by Psycho7; Mar 25, 2008 at 07:40 PM. Reason: Added more info
Psycho7 is offline   0 Reply With Quote
Old Mar 25, 2008, 07:43 PM   #2
CaptainZap
macrumors regular
 
Join Date: Jan 2007
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.
__________________
CaptainZap is offline   0 Reply With Quote
Old Mar 25, 2008, 07:45 PM   #3
TEG
macrumors 604
 
TEG's Avatar
 
Join Date: Jan 2002
Location: Langley, Washington
Send a message via ICQ to TEG Send a message via AIM to TEG Send a message via MSN to TEG Send a message via Yahoo to TEG Send a message via Skype™ to TEG
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
__________________
Apple and Dell are the only ones in this industry making money. They make it by being Wal-Mart. We make it by innovation, - Steve Jobs
The Tegian Zone-Glass Onion Radio
TEG is offline   0 Reply With Quote
Old Mar 25, 2008, 07:53 PM   #4
Psycho7
Thread Starter
macrumors newbie
 
Join Date: Mar 2008
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
Psycho7 is offline   0 Reply With Quote
Old Mar 25, 2008, 08:07 PM   #5
CaptainZap
macrumors regular
 
Join Date: Jan 2007
How is your program using the argument? Is it using it as a path that then accesses the file?
__________________
CaptainZap is offline   0 Reply With Quote
Old Mar 25, 2008, 08:13 PM   #6
Psycho7
Thread Starter
macrumors newbie
 
Join Date: Mar 2008
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.
Psycho7 is offline   0 Reply With Quote
Old Mar 25, 2008, 08:19 PM   #7
CaptainZap
macrumors regular
 
Join Date: Jan 2007
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.
__________________

Last edited by CaptainZap; Mar 25, 2008 at 08:35 PM.
CaptainZap is offline   0 Reply With Quote
Old Mar 26, 2008, 01:44 AM   #8
Psycho7
Thread Starter
macrumors newbie
 
Join Date: Mar 2008
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;
}

Last edited by Psycho7; Mar 29, 2008 at 07:21 AM.
Psycho7 is offline   0 Reply With Quote
Old Mar 26, 2008, 07:20 AM   #9
CaptainZap
macrumors regular
 
Join Date: Jan 2007
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.
__________________
CaptainZap is offline   0 Reply With Quote
Old Mar 26, 2008, 09:06 AM   #10
Psycho7
Thread Starter
macrumors newbie
 
Join Date: Mar 2008
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!
Psycho7 is offline   0 Reply With Quote
Old Mar 26, 2008, 10:07 AM   #11
TEG
macrumors 604
 
TEG's Avatar
 
Join Date: Jan 2002
Location: Langley, Washington
Send a message via ICQ to TEG Send a message via AIM to TEG Send a message via MSN to TEG Send a message via Yahoo to TEG Send a message via Skype™ to TEG
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
__________________
Apple and Dell are the only ones in this industry making money. They make it by being Wal-Mart. We make it by innovation, - Steve Jobs
The Tegian Zone-Glass Onion Radio
TEG is offline   0 Reply With Quote
Old Mar 26, 2008, 10:27 AM   #12
Psycho7
Thread Starter
macrumors newbie
 
Join Date: Mar 2008
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
Psycho7 is offline   0 Reply With Quote
Old Mar 26, 2008, 11:42 AM   #13
TEG
macrumors 604
 
TEG's Avatar
 
Join Date: Jan 2002
Location: Langley, Washington
Send a message via ICQ to TEG Send a message via AIM to TEG Send a message via MSN to TEG Send a message via Yahoo to TEG Send a message via Skype™ to TEG
When I use XCode, I find it easier to compile and run outside of XCode (only using it for the Syntax Highlighting).

TEG
__________________
Apple and Dell are the only ones in this industry making money. They make it by being Wal-Mart. We make it by innovation, - Steve Jobs
The Tegian Zone-Glass Onion Radio
TEG is offline   0 Reply With Quote
Old Mar 26, 2008, 04:00 PM   #14
CaptainZap
macrumors regular
 
Join Date: Jan 2007
Quote:
Originally Posted by Psycho7 View Post
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!
Sorry, I couldn't reply sooner but your post below answered your question.

Quote:
Originally Posted by Psycho7 View Post
/Users/myname/Desktop/Project 4/build/Release/test.obj
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
__________________
CaptainZap is offline   0 Reply With Quote
Old Mar 29, 2008, 07:20 AM   #15
Psycho7
Thread Starter
macrumors newbie
 
Join Date: Mar 2008
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!
Psycho7 is offline   0 Reply With Quote
Old Mar 29, 2008, 08:04 AM   #16
CaptainZap
macrumors regular
 
Join Date: Jan 2007
Quote:
Originally Posted by Psycho7 View Post
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!
Haha no problem, I'm just glad it worked :P
__________________
CaptainZap is offline   0 Reply With Quote
Old Dec 10, 2009, 09:03 PM   #17
Oats
macrumors regular
 
Join Date: Jan 2003
Location: New York
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?
Oats is offline   0 Reply With Quote
Old Dec 10, 2009, 09:18 PM   #18
Detrius
macrumors 68000
 
Join Date: Sep 2008
Location: Asheville, NC
Quote:
Originally Posted by Oats View Post
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?
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/Program...d_applications
Detrius is offline   0 Reply With Quote
Old Dec 11, 2009, 07:08 AM   #19
Oats
macrumors regular
 
Join Date: Jan 2003
Location: New York
Quote:
Originally Posted by Detrius View Post
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/Program...d_applications
I'm building a C++ application, so the Cocoa classes are not available to me... any other ideas?
Oats is offline   0 Reply With Quote
Old Dec 11, 2009, 12:28 PM   #20
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
Is this supposed to be a cross-platform application?
Eraserhead is offline   0 Reply With Quote
Old Dec 12, 2009, 12:08 AM   #21
Detrius
macrumors 68000
 
Join Date: Sep 2008
Location: Asheville, NC
Quote:
Originally Posted by Oats View Post
I'm building a C++ application, so the Cocoa classes are not available to me... any other ideas?
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.
Detrius is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Passing a text file into Xcode instead of using the command line (C language) hollabit Mac Programming 19 Mar 31, 2014 11:45 AM
Resolved: [XCode Beginner] Program not Showing in Screen UpsideDown iPhone/iPad Programming 13 Nov 6, 2013 07:50 PM
Program in Xcode won't run without net connection. impilot Mac Programming 4 Aug 28, 2012 03:47 PM
Multiplying array elements by 3. C program on Xcode Mugambo Mac Programming 3 Jul 26, 2012 02:02 AM
File copy program - queueing large file copies whitedragon101 OS X 6 Jun 27, 2012 06:47 AM

Forum Jump

All times are GMT -5. The time now is 09:28 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC