Empty file help

I got my program to work but I want to add an if statement where if the file is empty to print out a message to tell the user that its empty. Here is a small part of my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 int main()
{
     County countyList[COUNTY_MAX];
     int index=0;
     ifstream input;
     string fname;
     //Intro
     cout << "Welcome to the Tennessee Sales Tax Report! \n";
     cout << "Enter input file name.\n";
     cin >> fname;
     //opening file of correct
     input.open(fname.c_str());
     
     if(input.fail())
     {
          cout << "Input file " << fname << " does not exist.\n";
	  exit(0);
     }
     while (input >> countyList[index].county)
     {
          input >> countyList[index].county;
          index++;
     }
     //Close input file
     input.close(); 
}
Last edited on
you should be able to accomplish this with fseek. Doing something like fseek(file, 0, SEEK_END); int size = ftell(file);
what does this do? Int size = ftell(file);
http://www.cplusplus.com/reference/cstdio/ftell/. It gets the number of bytes from the beginning of the file.

it should be a long and not an int. This also has a potential of overflow. You can only get the size of files up to 2GB. This will not work for larger files. You will need to use the function fstat
Last edited on
Topic archived. No new replies allowed.