checking for files

This is a discussion on checking for files within the C++ Programming forums, part of the General Programming Boards category; what would i do to find if a file was already made? for example Code: fstream a_file("data.dat") return(a_file.open()); if(FALSE) etc....Please ...

  1. #1
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942

    checking for files

    what would i do to find if a file was already made?
    for example
    Code:
    fstream a_file("data.dat")
    return(a_file.open());
    if(FALSE)
    etc....Please help!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>what would i do to find if a file was already made
    Do you mean you want to check is a file exists?

    For a standard way, open it in read mode, if it fails, the chances are it doesn't exist (but there could be other reasons).

    For a non-standard way, use a compiler specific function that will give you additional information, maybe you have stat()?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    Do you mean you want to check is a file exists?
    Exactly. Then I want it to return true or false or something like that so I can use an if(FALSE) command.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Start from here (or something similar):
    Code:
      ifstream checker;
      
      checker.open("junk1.c");
      if (checker)
        cout <<"junk1.c exists" <<endl;
      else
        cout <<"junk1.c does not exist" <<endl;
      checker.close();
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  2. Overflow and range checking for mul/div
    By Elysia in forum C++ Programming
    Replies: 28
    Last Post: 06-06-2008, 03:09 PM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:59 PM
  4. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 07:57 PM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 10:05 AM

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