Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
const char *filename = "test.txt";
ifstream fin(filename);
char tmp[80];
while(!fin.eof()){
    int i = 0;
    fin.getline(tmp,80);
    while(tmp[i] != '\0'){
       std::cout << tmp[i];
       i++;
    }
    //if(fin.fail() && !fin.eof()){
      //  fin.clear();
        //if(fin.peek() == '\0')
          //   fin.get();
    //}
}

I don't know why the input istream is stopped.
i don't know why fin.getline(tmp,80) can't go on at the next time.
Thank you!
Posted
Updated 3-Apr-12 16:48pm
v4
Comments
JF2015 3-Apr-12 1:13am    
Added pre tags.
ThatsAlok 3-Apr-12 2:02am    
Have you tried debugging the problem, overall code looks fine to me

you are not incrementing the value of i by 1 as below(infinite loop)

C#
const char *filename = "test.txt";
ifstream fin(filename);
char tmp[80];
while(!fin.eof()){
    int i = 0;
    fin.getline(tmp,80);
    while(tmp[i] != '\0')
    {
       std::cout << tmp[i];
       i++;
    }
    std::cout<<"\n";
    if(fin.fail() && !fin.eof()){
        fin.clear();
        if(fin.peek() == '\0')
             fin.get();
    }
}


also changed the code to print one line as in the file
 
Share this answer
 
Comments
Resmi Anna 3-Apr-12 4:18am    
sorry....Hardly understood anything:(
Youming Lee 3-Apr-12 4:31am    
const char *filename = "test.txt";
ifstream fin(filename);
char line[80];
while(!fin.eof()){
int i = 0;
fin.getline(line,80);
while(line[i] != '\0'){
std::cout << line[i];
i++;
}
// if(fin.fail() && !fin.eof()){
// fin.clear();
// if(fin.peek() == '\0')
// fin.get();
}
}
i want to know why fin.fail() is set to 1?
but if using:
string line;
while(getline(fin,line))
std::cout << line;
it works. fin.fail() is not set to 1. What makes the difference?
i want to reading one line by line from a file. if using char line[81] or char * line = new char [81]; i must add the comment line or it will loop for ever. i don't understand 1. What reason sets the fin.fail() to 1.
2.if i complie the code as follows ,it still doesn't work.
while(!fin.eof()){
int i = 0;
fin.getline(line,80);
while(line[i] != '\0'){
std::cout << line[i];
i++;
}
fin.clear();
}
regardless of i has any value,(i assign i to 0.).line[i] always equals to '\0'. That is to say it didn't execute the "fin.getline(line,80);". it is the result of he fin.fail() is set to 1.
Thank you.Any help is precious to me.
[no name] 3-Apr-12 5:47am    
He found one bug - now you ask another different question???
The istream method getline(char *,int) would set failbit flag when it reads the maximum number of input characters. So,I must give the method to clear the failbit flag,then it can read next line.
Thank those who give me advice.
 
Share this answer
 
v2
In the mentioned code, you given the intialization i = 0; ie: always refer to the first character from the line. This needs to increment inside the loop and do the check. I think then it will work and print the whole file.

C#
while(tmp[i] != '\0')
    {
       std::cout << tmp[i];
       i++;
    }
 
Share this answer
 

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



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