Now, take a look at this code. I created two variables. One is a string and the other is an integer. This time, test.txt file has
As you already know, the string "test" cannot be stored in an integer variable, but in this program, I will force it. See what happens.
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
|
#include <iostream>
#include <fstream>
#include <string>
int main ()
{
std::ifstream inFile;
std::string fileName; // Stores the file name provided by user
std::string word;
int number;
std::cout << "Enter the name of text file you would like to open: ";
std::cin >> fileName;
fileName += ".txt"; // Add the extension
inFile.open ( fileName );
inFile >> number; // string can't be stored in number variable.
if ( inFile.fail () )
std::cout << "The state flag of file input stream is set to fail.\n";
else
std::cout << "The state flag of the file input stream is NOT set to fail.\n";
if ( inFile.bad () )
std::cout << "The state flag of file input stream is set to bad.\n";
else
std::cout << "The state flag of file input stream is NOT set to bad.\n";
if ( inFile.good () )
std::cout << "The state flag of file input stream is set to good.\n";
else
std::cout << "The state flag of file input stream is NOT set to good.\n";
if ( inFile.eof () )
std::cout << "The state flag of file input stream is set to eof.\n";
else
std::cout << "The state flag of file input stream is NOT set to eof.\n";
return 0;
}
|
Sample Run 1: Force the program to store a string in integer variable from the file and observe which bit is set
1 2 3 4 5 6 7 8
|
Enter the name of text file you would like to open: test
The state flag of file input stream is set to fail.
The state flag of file input stream is NOT set to bad.
The state flag of file input stream is NOT set to good.
The state flag of file input stream is NOT set to eof.
Process returned 0 (0x0) execution time : 1.702 s
Press any key to continue.
|
Sample Run 2: Now, change the code so that the program stores the string, "test" in the string variable and observe the state flag. Notice that now, the input stream is set to EOF (End of File). That is because there was only one word in the text file and I just yanked it using >>, so the eof bit was set. Because the string was stored in an appropriate variable, fail bit was not set.
1 2 3 4 5
|
Enter the name of text file you would like to open: test
The state flag of the file input stream is NOT set to fail.
The state flag of file input stream is NOT set to bad.
The state flag of file input stream is NOT set to good.
The state flag of file input stream is set to eof.
|
Sample Run 3: Now, I am going to use the following line and attempt to read from the same file even though there is no more items.
inFile >> word >> word;
1 2 3 4 5 6 7 8
|
Enter the name of text file you would like to open: test
The state flag of file input stream is set to fail.
The state flag of file input stream is NOT set to bad.
The state flag of file input stream is NOT set to good.
The state flag of file input stream is set to eof.
Process returned 0 (0x0) execution time : 1.492 s
Press any key to continue.
|
This time, both the fail and eof bits are set. EOF was set because with the first inFile >> word; triggered the end of file. The extra inFile >> word; has triggered the fail bit.