Thread: fopen filename parameter

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    fopen filename parameter

    I'm not sure if this question has been asked already recently anyway (at least I'm having trouble finding a topic similar to my question). For the filename parameter of fopen function, if I'm not specifying a full filepath, does the program assume that the filename is in the same directory as itself?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Concept of a working directory. Most of the time, yes, the working dir will be the same dir the exe is located in, but not necessarily.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    How would I set the working directory then (if i'm not using it in an IDE)? Would it be safe to explicitly state the path in filename?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you're on Unix or the like, you could set it via chdir().

    Yes, it would be safe to set it via the filename, however, you could just assume a relative location to the working directory.

  5. #5
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    It looks like it: http://www.cplusplus.com/reference/c...dio/fgets.html It doesn't take long to try it out though. E.g. the following works for me when "myfile.txt is in the same directory as the path for cmd. Otherwise it can't find the program, so when given a relitive path, it serces in the current directory. Try out other things, like putting the file to open in the system root and c if it finds it. Note opening with write access creates the file if non-existant:
    Code:
    /* fopen example from site above*/
    #include <stdio.h>
    int main ()
    {
      FILE * pFile;
       char mystring [100];
      pFile = fopen ("myfile.txt","r");
      if (pFile == NULL) perror ("Error opening file");
      else
      {
         fgets (mystring , 100 , pFile);
         puts (mystring);
         fclose (pFile);
      }
      return 0;
    }
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The working directory is normally the directory your command line "shell" is currently in when the program is invoked.

    Windows on the other hand seems to make a complete mess of it.
    If you double-click on an exe, it's one place
    If you drag/drop a file onto it, it's another place
    and so on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 09-23-2008, 12:59 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. cannot start a parameter declaration
    By Dark Nemesis in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2005, 02:09 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM