Thread: File i/o

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2

    File i/o


    How do I locate the files using the fopen function?

    This is the example given in the textbook:
    Code:
    input = fopen ("b:input.txt", "r");
    So I created the file input.txt in the same folder where the executed file exists, but there was an error.
    Next I copied the address from the property of the text file and entered
    Code:
    input = fopen ("C:\Programming\2013-06-06\2013-06-06\input.txt", "r");
    but this didn't seem to work either.

    Where am I supposed to store my files, and how am I suppose to locate them?
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,552
    Rep Power
    2337
    In order to use backslashes in a path you must use \\ for each \.

    https://en.wikipedia.org/wiki/Escape_sequences_in_C
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    In C/C++ the '\' character is considered the escape character, so to use this character in a string you need two for each occurrence.

    Code:
    input = fopen ("C:\\Programming\\2013-06-06\\2013-06-06\\input.txt", "r");
    Also note that using the "r" open command the file must exist, it will not be created if it doesn't exist.

    Also the working directory is not always located in the directory where the executable is located. If you're running the program thru an IDE the working directory is often where the project file is located.

    Jim
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    Thank you, I got it fixed now.

    Also the working directory is not always located in the directory where the executable is located.
    I'll keep that in mind.

IMN logo majestic logo threadwatch logo seochat tools logo