Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I use dec-cpp and the code as such ("result.txt" being present) continues on forever. It should not. Please help.
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
      ifstream fin_sort("result.txt");
     
     char txt[2]="";
     while(!fin_sort.eof()){
                              fin_sort.getline(txt,2);
                              cout << " -" << txt << "- " << endl;
                              
                              }
     fin_sort.close();
 
 return 0;   
}
Posted

 
Share this answer
 
Thank you for your answer!

however after some testing I found out that if I call a getline function which reads less characters than there are available on the line in the file, the eof() dies.

ANSI txt file:
ex
am
pl
e
example
ex
am
pl
e


output:
ex
am
pl
e










...


This is one big mess. Can anyone explain this to me?
It's just that I would like to understand.

Thanks
 
Share this answer
 
Comments
Cedric Moonen 26-Jan-11 9:08am    
Please read the documentation of GetLine, it is stated explicitely that: "If the function stops reading because this size is reached, the failbit internal flag is set". They are talking about the second parameter of the GetLine function (you pass 2). This is also the reason why it looping infinitely: the fail bit is set so no data is read anymore but the end of file is not reached. Check the function documentation here: http://www.cplusplus.com/reference/iostream/istream/getline/
Firstly, the buffer size includes the NULL termination character, so char txt[2]=""; can actually only store 1 character and the null terminator character as per MSDN[^]

Secondly, if there is not enough characters in the buffer to store the line of text, then the state is flagged wit the FAIL BIT. This bit needs to be cleared before any (not just getline) reading can continue.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
    ifstream fin_sort("result.txt");
    char txt[3] = ""; //Allow space for 2 characters and a NULL terminator
    while (!fin_sort.eof()) {
        fin_sort.getline(txt, sizeof(txt)); //It is usually better to use sizeof() in case you change the size later
        cout << " -" << txt << "- " << endl;
        if (fin_sort.fail()) {
            cout << "Fail bit set. Clearing it for read to resume" << endl;
            fin_sort.clear(); //Clear the fail bit (Warning: this function can also clear the EOF bit if not used properley)
        }
    }
    fin_sort.close();
    return 0;
}
 
Share this answer
 
string fileName2;
cout << "Input a file to open: ";
cin >> fileName2;

ifstream fin2(fileName2.c_str());

if(fin2) //checking if file exists
{
	cout << "Current file contains:\n";
	
	while(fin2.get(ch))
	{
		cout << ch;
	}
	
	cout << "\n***End of File***\n\n";
}
else
{
	cout << "File does not exist! Therefore it was created\n\n";
}
 
Share this answer
 
That's one of the most trick thing of the C++ library, because of certain names, suggesting things that are not what they effectively are.

The eof function, according to the reference[^]
"returns true if the eofbit stream's error flag has been set by a previous i/o operation. This flag is set by all standard input operations when the End Of File is reached in the sequence associated with the stream"

The "End OF File", here, is not the "physical end", but a particular ASCII character (Ctrl+Z) that is supposed "to boldly be where all characters are before" (yes: Captain Kirk participate in the ASCII specs). But, since when the teletype world has been "superseeded" by computers, Ctrl+Z is just a byte like any other: it may have character after, and it may be even not present.

Instead of check for EOF, you shold check for "bad()", that returns the state of a bit that sets up when reading become impossible (either because there is nothing more to read or because what is read is not useful to fill re variable it is directed to).

This char is done by istream::operator void*(), by returning NULL if the stream is "bad". So that stream can be an exit condition for a loop.

In your case
while(finsort)
{ .... }

Should behave as expected.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900