Like Tree1Likes
  • 1 Post By Salem

Check whether file exists and exceeds a certain size

This is a discussion on Check whether file exists and exceeds a certain size within the C++ Programming forums, part of the General Programming Boards category; Hi guys, I am familiar with checking whether a file exists or not... Something like: Code: int File_Exist(string s) { ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Check whether file exists and exceeds a certain size

    Hi guys,

    I am familiar with checking whether a file exists or not...
    Something like:
    Code:
    int File_Exist(string s)
    {   
      ifstream  file;
      int       x = 1;
    
      file.open(s,ios::in|ios::binary);
      if (file.fail()) x = 0;
      else file.close();  file.clear();
      return(x);
    }
    However,

    Once establishing a file exists I want to check whether it exceeds a certain size... or rather that is contains some data.
    That's basically it. If it contains any data then I do one things. If it contains nothing then I do another.

    What do you think? Any suggestions?

    cheers

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    Using the existing API, you could seekg to the end of an existing file, then use tellg to find out how big the file is.

    stat(3): file status - Linux man page
    Or if you can, you can figure out whether it exists and it's size all in one call.
    strokebow likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    If I use something similar:

    Code:
     
      ifstream  f;
      int       x = 0;
    
      f.open(s,ios::in|ios::binary);
      f.seekg(0, ios::end);
      x = f1.tellg();
    This should work, do you think?

    ** Actually how should I deal with the return value of tellg??

    thanks

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    IS this OK for your liking???

    Code:
    x = static_cast<int>(f.tellg());

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to check if a file exists?
    By elninio in forum C++ Programming
    Replies: 13
    Last Post: 07-31-2008, 03:20 PM
  2. Check If File Exists On FTP
    By stickman in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2006, 06:06 PM
  3. How to check if a file exists
    By ElastoManiac in forum C++ Programming
    Replies: 10
    Last Post: 12-05-2005, 05:20 PM
  4. How to check if a file exists
    By ElWhapo in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2004, 05:16 PM
  5. check if the file exists
    By ipe in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 09:18 AM

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