Thread: Question for a friend on file checking.

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Question for a friend on file checking.

    He wants to know how to check if a file exists and how would you check to see if that same file was empty as in having no data in it.

    He basically wants to know which standard c++ library you are supposed to use?

    he was thinking something like this
    Code:
    ifstream inFile("a:data", ios::nocreate)
    if(!infile.Is_open())
    //....whatever

    he says
    "The nocreate doesn't seem to be a member of ios anymore, giving errors. How can you do the same using another function, and not using stdio.h from C."

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    This is what I usually do (since I don't know of any function that checks if a file exists):
    Code:
    bool CheckFile(char* FileName)
    {
       ifstream File();
       File.open(FileName, ios::in);
       if(!File.fail())
       {
          File.close();
          return true;
       }
       return false;
    }
    Since an infile-stream doesn't create a new file if it doesn't exist (what would it read ), it will always fail if the file doesn't exist.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Regarding file contents, seek the data pointer to the end of the file and get get the seek target size.

    seekg(ios::end)
    tellg()

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM