PDA

View Full Version : Passing a file along with the program in Xcode 3




Psycho7
Mar 25, 2008, 07:39 PM
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



CaptainZap
Mar 25, 2008, 07:43 PM
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.

TEG
Mar 25, 2008, 07:45 PM
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

Psycho7
Mar 25, 2008, 07:53 PM
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

CaptainZap
Mar 25, 2008, 08:07 PM
How is your program using the argument? Is it using it as a path that then accesses the file?

Psycho7
Mar 25, 2008, 08:13 PM
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.

CaptainZap
Mar 25, 2008, 08:19 PM
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.

Psycho7
Mar 26, 2008, 01:44 AM
/*
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;
}

CaptainZap
Mar 26, 2008, 07:20 AM
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.

Psycho7
Mar 26, 2008, 09:06 AM
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!

TEG
Mar 26, 2008, 10:07 AM
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

Psycho7
Mar 26, 2008, 10:27 AM
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

TEG
Mar 26, 2008, 11:42 AM
When I use XCode, I find it easier to compile and run outside of XCode (only using it for the Syntax Highlighting).

TEG

CaptainZap
Mar 26, 2008, 04:00 PM
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.

/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

Psycho7
Mar 29, 2008, 07:20 AM
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!

CaptainZap
Mar 29, 2008, 08:04 AM
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

Oats
Dec 10, 2009, 09:03 PM
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?

Detrius
Dec 10, 2009, 09:18 PM
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/Programming_Mac_OS_X_with_Cocoa_for_beginners/Document-based_applications

Oats
Dec 11, 2009, 07:08 AM
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/Programming_Mac_OS_X_with_Cocoa_for_beginners/Document-based_applications
I'm building a C++ application, so the Cocoa classes are not available to me... any other ideas?

Eraserhead
Dec 11, 2009, 12:28 PM
Is this supposed to be a cross-platform application?

Detrius
Dec 12, 2009, 12:08 AM
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.