Addressing a file path in C

This is a discussion on Addressing a file path in C within the C Programming forums, part of the General Programming Boards category; Hello. I have recently moved from programming in C in Windows (Using Visual Studio) to programming C in a Mac ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    3

    Addressing a file path in C

    Hello. I have recently moved from programming in C in Windows (Using Visual Studio) to programming C in a Mac (using Xcode). I am still learning to master Xcode, but one of the first problems I encounter is one related with writing in a file.

    In Windows I had no problem in writing:
    [code]
    FILE *outfile;
    outfile = fopen("UnifE-n51-k5", "w");
    fprintf(outfile,"a \t b \t ajuste \t media \n");
    fprintf(outfile,"================================= =\n");
    fclose(outfile);
    [\code]

    However when trying to copy that piece of code into my Mac program I have to give the whole path to the outfile.


    [code]
    FILE *outfile;
    outfile = fopen("/Users/Marta/Documents/VRPStochastic/UnifE-n51-k5", "w");
    fprintf(outfile,"a \t b \t ajuste \t media \n");
    fprintf(outfile,"================================= =\n");
    fclose(outfile);
    [\code]

    I wonder if that is what I really have to do or if there is another short way of doing it, since with the first example on Xcode the address for the outfile is NULL.

    It would be very helpful for me to know the short way, if any, since I work with many C programs (and with someone that uses Windows Visual Studio) and it is really annoying to remember the whole path all the time, or to change all the programs my colleage does so I can run them on my mac.

    Thans a lot

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    982
    Note that a forward slash / is used when closing tags, not a backslash \.

    Are you sure your program is being run from the /Users/Marta/Documents/VRPStochastic directory? If so, you should be able to simply fopen("UnifE-n51-k5", "w") with no explicit path. If it is being run from some other directory (e.g. the directory you were in when you started the IDE), you'll need to either use a full path or make sure the directories match, by moving either the program or the file.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    Thanks for the tip on the code editing (bad usage due to latex habits )

    As for the question if I only write fopen ("namefile"."w") it does not create any file anywhere. Since I am programming in Xcode, the Xcodeproject is in the directory I post before. The .c files are in a folder in /Users/Marta/Documents/VRPStochastic called VRPStochastic. However if I don't specify any path in the fopen command no file is created.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specify a path for write file
    By Ali.B in forum C Programming
    Replies: 2
    Last Post: 08-13-2009, 04:36 PM
  2. delete file(s) using path and name
    By ashraf24 in forum C Programming
    Replies: 7
    Last Post: 02-03-2009, 09:25 AM
  3. Getting the path file of your program?
    By RedZone in forum C++ Programming
    Replies: 13
    Last Post: 07-23-2006, 11:25 AM
  4. Getting file name off path
    By cgod in forum C++ Programming
    Replies: 6
    Last Post: 09-22-2005, 10:45 AM
  5. File Path Setting
    By blue_gene in forum C Programming
    Replies: 11
    Last Post: 12-23-2003, 06:04 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21