how can i check for empty text file?

can anyone tell me how can i create a function like:

if ("the txt file is blank")
dosomething;


which is checking if the text file is empty and then do something.
What do you mean by "blank"?

A size of zero?

Or only lines of whitespace (spaces, newlines, tabs, etc)?

Try:

In a separate function, create a variable, increment it every time a line is found (getline()). Then return the amount of lines counted. If its 0, then search the line for characters. If none are found you have a blank file.

Or you might be able to use sizeof.

Catch result.
something like:?

1
2
3
4
5
6
7
8
9
10
int length;
ifstream filestr;

filestr.open("fileYouWantToTest.txt", ios::binary); // open your file
filestr.seekg(0, ios::end); // put the "cursor" at the end of the file
length = filestr.tellg(); // find the position of the cursor
filestr.close(); // close your file

if ( length == 0 ){...}
else {...}


[edit] Note: length is the number of characters/bytes in the file
Last edited on
for #include<string.h>

if (strlen(a) == 0)
statement;
else
statement;

for #include <string>

string boot;

if (boot.empty() = 1) // if boot equals true its empty
statement;
else
statement;

oh a file... oops misread
Last edited on
Well, if you really mean text file, then the following should do what you want for ASCII, UTF-8, UTF-16, and UTF-32:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <ciso646>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

//----------------------------------------------------------------------------
bool is_textfile_empty( const char* filename )
  {
  string   s;
  ifstream f( filename, ios::binary );

  // Check for UTF-8 BOM
  if (f.peek() == 0xEF)
    {
    f.get();
    if (f.get() != 0xBB) return false;
    if (f.get() != 0xBF) return false;
    }

  // Scan every line of the file for non-whitespace characters
  while (getline( f, s ))
    {
    if (s.find_first_not_of(
      " \t\n\v\f\r" // whitespace
      "\0\xFE\xFF"  // non-printing (used in various Unicode encodings)
      ) != string::npos)
      return false;
    }

  // If we get this far, then the file only contains whitespace
  // (or its size is zero)
  return true;
  }

//----------------------------------------------------------------------------
int main( int argc, char** argv )
  {
  if ((argc < 2)
  or  (string( "?\0/?\0--help", 11 ).find( argv[ 1 ] ) != string::npos))
    {
    cout <<
      "usage:\n  " << argv[ 0 ] << " HELPOPT | FILELIST\n\n"

      "  HELPOPT is one of\n"
      "    ?      /?      --help\n\n"

      "  FILELIST is a list of filenames separated by whitespace.\n\n"

      "This program tells you whether or not a given text file is 'empty'.\n"
      "An empty text file is one that contains no characters other than\n"
      "ASCII whitespace for UTF-8, UTF-16, and UTF-32 encoded files.\n\n";
    return 1;
    }

  for (int n = 1; n < argc; n++)
    cout << (is_textfile_empty( argv[ n ] ) ? "empty: " : "full:  ")
         << argv[ n ]
         << endl;

  return 0;
  }


I haven't tested this thoroughly... also, it only checks for ASCII whitespace characters 0, 9..13, and 32. There are a few other Unicode characters you may wish to test for...

Hope this helps.
misty you can gather a line of text from the text file.
If that file is empty with the string.size() command or sizeof() command then it is blank.

Topic archived. No new replies allowed.