Detecting empty file situation.

This is a discussion on Detecting empty file situation. within the C Programming forums, part of the General Programming Boards category; Hello: I'm in a small bind. In my program I'm opening a file that may be empty. I can write ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    13

    Detecting empty file situation.

    Hello:

    I'm in a small bind. In my program I'm opening a file that may be empty. I can write the code around my problem of detecting whether or not a file is empty by setting my arrays to none zero values but I'd rather use a command if there is one. I checked through the tutorials and didn't find one. I know NULL is used for failing to open a file but is there a operator for a file that has no data in it? Any input will be appreciated. Thanks.

    Icy

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, a blank file should have only the EOF character. So you could use feof() to check if fp reaches end of file. Or fgetc() and check for EOF.

    I don't know any ready function that does that. But it is like 1 line of code

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Try something like this:
    Code:
    int main (void)
    {
    	FILE *fp;
            char *path = "path\\to\\file";
    	long size;
    
    	fp = fopen(path, "r");
    
    	if (fp)
    	{
    		fseek (fp, 0, SEEK_END);
    		size = ftell(fp);
    		printf("Size of the file in bytes: %lu\n", size);
    		fclose(fp);
    	}
    
    	return 0;
    }
    If size is ever 0, you know the file is empty.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    Quote Originally Posted by C_ntua View Post
    Well, a blank file should have only the EOF character. So you could use feof() to check if fp reaches end of file. Or fgetc() and check for EOF.

    I don't know any ready function that does that. But it is like 1 line of code
    I've only used feof() in the form of a !feof(), I just tried it with the following code, and it didn't execute the code even though the file was empty. Any ideas?

    Code:
        if(feof(IF))
        {
        printf("Data file is emtpy, exiting the program");
        exit(0);     
        }

  5. #5
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Quote Originally Posted by xIcyx View Post
    I've only used feof() in the form of a !feof(), I just tried it with the following code, and it didn't execute the code even though the file was empty. Any ideas?

    Code:
        if(feof(IF))
        {
        printf("Data file is emtpy, exiting the program");
        exit(0);     
        }
    If you want to use the feof method, try this:
    Code:
    int main (void)
    {
    	FILE *fp;
            char *path = "path\\to\\file";
    	long size = 0;
    
    	fp = fopen(path, "r");
    
    	if (fp)
    	{
    		while (!feof(fp))
    		{
    			fgetc(fp);
    			size++;
    		}
    		printf("Size of the file in bytes: %lu\n", size);
    		fclose(fp);
    	}
    
    	return 0;
    }
    Keep in mind though that this way, an eof char will be returned, so it will say it contains one byte.

    By the way, on http://www.cplusplus.com, they have many examples of functions and your case is one of them.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    Thank you for the input carrotcake.

    This is what I'm leaning twoards, I was writing my first reply while you posted so did not see it first.

    Code:
        if(IF)
        {
    		fseek (IF, 0, SEEK_END);
    		lSize = ftell(IF);
    		if(lSize == 0)
    		{
    		fclose(IF);
                    printf("\nInput file is empty, exting the program\n");
    		system("PAUSE");
    		exit(0);
        }
    }
    Edit: I fixed my problem, no clue why it didn't work before. Thanks for the help guys, and the referral to a new site for resources on C.
    Last edited by xIcyx; 06-18-2008 at 07:52 PM. Reason: Fixed my problem.

  7. #7
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Are you saying that your function is pausing, and then terminating? Or are you trying to say that your function isn't pausing?

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by xIcyx View Post
    I've only used feof() in the form of a !feof(), I just tried it with the following code, and it didn't execute the code even though the file was empty. Any ideas?

    Code:
        
    if(feof(IF))
    {
        printf("Data file is emtpy, exiting the program");
        exit(0);     
    }
    Yeah. You would need to execute first an fgetc() then the above code.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    Quote Originally Posted by carrotcake1029 View Post
    Are you saying that your function is pausing, and then terminating? Or are you trying to say that your function isn't pausing?
    I fixed my problem, I honestly think it was working and because of all the password verification stuff I have getting into the program I took it as executing, just a stressed out mistake.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    If handling EOF is an issue, you're probably doing it wrong.

    Consider
    Code:
    int ch;
    while ( (ch=fgetc(fp)) != EOF ) {
      // do stuff here
    }
    or
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, sizeof buff, fp ) != NULL ) {
      // do stuff here
    }
    If the file is empty to begin with, the loops will exit without doing any "stuff"
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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