Thread: skipping empty files using ifstream

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    16

    Cool skipping empty files using ifstream

    Hey everyone, i was wondering how i could skip over empty files in a directory. For example if i have a loop that is just reading in all the .txt files in a directory, and i want the empty ones to be left alone, and alterations done on the non empty files.

    So basically what im looking for is a built in function that will check to see if a file is empty. Simple question, i just can't seem to find the right function.


    thanks alot.

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    if windows you could try this

    Code:
    #include <iostream>
    #include <sys/stat.h>
    
    using namespace std;
    
    int main()
    {
    	struct stat results;
    	    
    	if (stat("BuildLog.htm", &results) == 0)
    	{
    	    cout << results.st_size << endl;
    	}
    	cin.get();
    	return 0;
    }

    of it it has to be fstream related this

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    16
    it is in C++ programming.

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    that is c++

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    16
    im not sure i understand what you have there. The .htm file, can it be replaced by any extension like .txt. what exactly does the stat() function do...

  6. #6
    Banned
    Join Date
    Jun 2005
    Posts
    594
    it find the fiel size why dont you run that, and yea
    you coudl use any char array, or you can use
    string wtih .c_str() on the end so that you can cycle thru the
    name you find in a loop, and then do a statment like

    if filesize > 0 do this

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Well if you want to strict fstream and such, you can like;

    Code:
    ifstream filehandle("your text files", ios::in | ios::binary  |ios::ate);
    size = filehandle.tellg();
    Then test size-1 against 0. And I'm sure you could read() or get() from the stream and test for an immediate EOF and get the same effect. According to MSDN we and the CRT have ourselves a filelength function defined in io.h, dunno, and can be read about here: http://msdn.microsoft.com/library/de...elengthi64.asp and then of course if we are limiting ourselves to windows you can use the GetFileSize[Ex] function.

  8. #8
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Wouldn't you want to use a seekg first to hit the end of the file by using
    Code:
    filehandle.seekg( 0, ios_base::end );
    Then calling a tellg?
    Last edited by dpro; 08-11-2005 at 11:34 AM. Reason: adding in code tags for esier reading

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    16
    i got it guys,

    i used the .eof() function. The reason i couldnt get it working before is because i was checking it before i tried to read anything in, so the .eof() wasnt getting set to true. When i put the getline() statement before the .eof() everything worked fine.


    Thanks for everything, its much appreciated.

  10. #10
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    As was stated in a previous thread of yours, do not use eof() for loop termination.

    Here

    Its better to check the return value of a getline than using eof.

  11. #11
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    From what I understand (not much) ios::ate opens it in append mode or something, with the file pointer at the end of the file anyways? std::cout << filehandle.tellg(); works fine for me. Anyways, it seems he found a somewhat viable solution, I'll leave it be.

  12. #12
    Registered User
    Join Date
    Jul 2005
    Posts
    16
    Quote Originally Posted by Tonto
    From what I understand (not much) ios::ate opens it in append mode or something, with the file pointer at the end of the file anyways? std::cout << filehandle.tellg(); works fine for me. Anyways, it seems he found a somewhat viable solution, I'll leave it be.


    Somewhat viable? Is there something wrong with the way i solved this problem. Is yours a better solution? I couldnt see any problem with the way wrote the code. The problem was solved with one If satement
    Code:
    if(!ins.eof()){
    .
    .
    .
    }
    So if the file is empty, it will skip all the processing and go on to the next file...

    If there is anything wrong with that please let me know...

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, this is precisly the kind of code which fails so miserably on empty files

    If you did
    while ( some_function_which_reads_from_the_file() != whatever_function_returns_on_end_of_file )
    whether it be fin.read or fin.get or whatever, and you TESTED the return result to determine success or failure, you wouldn't have all this special hackery for empty files.

    Eg.
    while ( fgets( buff, sizeof buff, in ) != NULL )
    Is the archetype reading a text file line by line in C.
    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.

  14. #14
    Registered User
    Join Date
    Jul 2005
    Posts
    16
    Quote Originally Posted by dpro
    As was stated in a previous thread of yours, do not use eof() for loop termination.

    Here

    Its better to check the return value of a getline than using eof.

    yes... i understand the error of my ways there. That was an old problem long fixed. This is a different section of code now to deal with empty files. So i do not crash my program when it tries to process these files. So basically what i did was put the .eof() if statement around all the file processing code so that if the file was empty, it would skip it, and go to the next file...

  15. #15
    Registered User
    Join Date
    Jul 2005
    Posts
    16
    Quote Originally Posted by Salem
    Yes, this is precisly the kind of code which fails so miserably on empty files

    If you did
    while ( some_function_which_reads_from_the_file() != whatever_function_returns_on_end_of_file )
    whether it be fin.read or fin.get or whatever, and you TESTED the return result to determine success or failure, you wouldn't have all this special hackery for empty files.

    Eg.
    while ( fgets( buff, sizeof buff, in ) != NULL )
    Is the archetype reading a text file line by line in C.


    I see what your getting at here, and that seems to be a good solution. However i am still unsure of why my code fails so miserabley... It has passed every test that i have thrown at it... What exatly is wrong with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  2. accessing all files in a folder.
    By pastitprogram in forum C++ Programming
    Replies: 15
    Last Post: 04-30-2008, 10:56 AM
  3. ifstream files
    By Stonehambey in forum C++ Programming
    Replies: 39
    Last Post: 03-12-2008, 01:51 PM
  4. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 12:59 AM
  5. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM