开发者

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 activeexecu开发者_运维百科table. 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?


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:

C Xcode question

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜