Michael
Michael

Reputation: 61

C Xcode question

I am trying to pass arguments to the command line in xCode. I have looked up this issue and have found that I need to set the working directory to the path that the file is in. Also I have to add the arguments to the arguments tab under project- edit activeexecutable. I have also done this.

I added michael.txt twice.

/* This file is saved as readtext.c, compiled as readtext */ 
#include <stdio.h> 
void main(int argc, char *argv[]) 
{ 
    FILE *fin; 
    char buffer[100]; 
    printf("Michael Mazur\n"); 
    if (argc != 2) {printf("Usage: %s filename\n", argv[0]); exit(1);} 
    fin = fopen(argv[1], "r"); 
    if (!fin) {printf("Unable to open %s\n", argv[1]); exit(1);} 
    while (fgets(buffer, 99, fin)) fputs(buffer, stdout); 
    fclose (fin); 
}  

I keep reaching the case that there are not 2 arguments being passed. I also ran a little test program and it keeps returning that I only have 1 argument being passed no matter how many I add. Any help?

Upvotes: 2

Views: 2278

Answers (1)

Nicholas Riley
Nicholas Riley

Reputation: 44321

argv[0] (path to the executable) counts in argc, so if you add michael.txt twice, argc will be 3. A slightly longer description is here. (In general, when something is misbehaving like this, either use a debugger to check the values of all the variables or print them out.)

Make sure both arguments are checked and on separate lines, like this:

picture of xcode

Also, in future please mention what version of Xcode you're using; I think from your description it's 3.x, so that's how I answered the question. The user interface varies pretty substantially between versions.

Upvotes: 4

Related Questions