Closed Thread
Results 1 to 5 of 5

Thread: Check if a file exists

  1. #1
    Lop's Avatar
    Lop
    Lop is offline Speaks fluent binary
    Join Date
    May 2006
    Posts
    1,178
    Rep Power
    30

    Check if a file exists

    Is there a function to check if a file exists?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    icepack's Avatar
    icepack is offline Programmer
    Join Date
    Jul 2006
    Location
    North Carolina
    Posts
    115
    Rep Power
    21
    Not so much a function, but a way
    Say you are trying to open a file to write to
    you'd need to #include <fstream>

    then to open the file use:
    Code:
    ofstream myFile("local location of file");
    but you always have to check if that file actually exists, or else it's be useless. so
    Code:
    if (! myFile)
    {
    cout << "Error\n";
    return -1;
    }
    is that what you wanted? i imagine you could just use that even if you didn't want to write to the file.

  4. #3
    Join Date
    Jul 2006
    Posts
    10
    Rep Power
    0
    You could also use the WIN API call "_stat()" to check on file status. it returns a -1 on error / file not found

    Code:
    int _stat( const char *path, struct _stat *buffer );

  5. #4
    Frantic's Avatar
    Frantic is offline Learning Programmer
    Join Date
    May 2006
    Posts
    91
    Rep Power
    0
    Never heard of the WIN API. How do I add that to my code?

  6. #5
    TkTech's Avatar
    TkTech is offline The Crazy One
    Join Date
    Jun 2006
    Location
    Canada
    Posts
    1,412
    Blog Entries
    1
    Rep Power
    31
    Frantic, if youve never heard of it, then just stick to standard c++

    Code:
    #include <fstream>
    
    void main( void )
    {
      ifstream fin("myfile.ini" , ios::in );
      ofstream fout;
      if( fin.fail() ) //File error
     {
        //So create the file
         fout.open("myfile.ini" , ios::out | ios::trunc );
         //File it with the default text/binary
         fout << "Whateveryouwantasdefault";
        //Close and clear the file write buffer
         fout.close();
         fout.clear();
        //And reopen the file
         fin.open("myfile.ini" , ios::in );
      }
       //Now do your normal operations as if nothing ever happend
       fin.read((char *)&bla , sizeof(blastruct));
       fin.close();
    }

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Check if multiple remote files exists HELP! (Using curl)
    By SeanStar in forum PHP Development
    Replies: 7
    Last Post: 12-17-2010, 09:06 AM
  2. Linux/Bash: Check if a file exists
    By Tor in forum Bash / Shell Scripting
    Replies: 4
    Last Post: 07-10-2009, 11:28 AM
  3. Check if a file exists?
    By Lop in forum Pascal and Delphi
    Replies: 3
    Last Post: 01-06-2007, 10:54 PM
  4. Check if a File Exists
    By NeedHelp in forum C# Programming
    Replies: 4
    Last Post: 08-07-2006, 03:20 PM
  5. Check if a URL Exists
    By Chan in forum PHP Development
    Replies: 1
    Last Post: 07-13-2006, 10:54 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts