1

I've written a super simple C command line tool that's supposed to read a file out from the path specified in argv[1]. I'm debugging using Xcode on OS X, so the working directory at the time of running is an Xcode-created directory separate from the file I want to read. (Xcode plugs in the arguments automatically, so that's not the problem.) I suspect the problem is related to my not understanding how fopen works. When I put in a path name, is it relative to the working directory? Will it work if I use the absolute path?

I've checked this post, this one, and this one, but they don't seem to help me in this case.

Here's the code I'm using - it's in the main function:

for (int i = 0; i < argc; i++)
    printf("arg%i: %s\n", i, argv[i]);

if (argc > 1)
{
    //Open the file specified by the first argument
    system("pwd");
    FILE *file = fopen(argv[1], "r");    //Always returns NULL

    if (file)
        //This isn't ever reached
    else
        printf("%s\n", strerror(errno));        
}

The output looks like this:

arg0: /Users/home/Library/Developer/Xcode/DerivedData/VSSequenceAlignment-avlfehlrtqiybygqfgueezoysnyi/Build/Products/Debug/VSSequenceAlignment
arg1: C:/Users/venkateshsivaramanDP/Documents/test/testdoc.txt
/Users/venkateshsivaramanDP/Library/Developer/Xcode/DerivedData/VSSequenceAlignment-avlfehlrtqiybygqfgueezoysnyi/Build/Products/Debug
No such file or directory

After changing the arg1 to use forward slashes it hasn't changed the error. Admittedly it's a step closer to the actual problem.

Community
  • 1
  • 1
architectpianist
  • 2,562
  • 1
  • 19
  • 27

2 Answers2

1

Your path is wrong; you are using a Windows path.

Compare your supplied path to arg0, specifying the path to the executable. arg1 has to be in the same style.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Femaref
  • 60,705
  • 7
  • 138
  • 176
  • If you mean the backslashes, changing them around didn't help either way. I still get the same error. – architectpianist Sep 20 '13 at 21:54
  • unix paths don't start with `C:`. go to the file you want to open in terminal and execute `echo $(pwd)/file_you_want_to_open`. This is the path you need to provide. – Femaref Sep 20 '13 at 21:57
  • There we go! Thank you, it turned out to be the C: causing the problem. If you want to add that to your answer, I'll go ahead and accept it. Because I may need to use an external disk to store the file I want to read in the future, how would I go about supplying that path? – architectpianist Sep 20 '13 at 22:02
  • That's entirely depending on the OS. in OSX, such volumes are in `/Volumes/`, while in others, like ubuntu or debian, it usually is in `/mnt/`. – Femaref Sep 20 '13 at 22:11
0

Yes, absolute paths work with fopen, but that's not your problem. I think that your problem comes from the content of arg[1], and I think it contains C:\Users\home\Documents\test\testdoc.txt which is a Windows path and you're on OS X.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Farouq Jouti
  • 1,657
  • 9
  • 15