Home > Back-end >  C using getline (fin, STR) reads the text file object of ifstream fin each line, what kind of writin
C using getline (fin, STR) reads the text file object of ifstream fin each line, what kind of writin

Time:11-22

C + + using getline (fin, STR) reads the text file object of ifstream fin each line, what kind of writing is good, the result right?
Ifstream fin (" a.t xt ");
String STR.

1,
while(! Eof ())
fin.{
Getline (fin, STR);
cout}
2,
Getline (fin, STR);
while(! Eof ())
fin.{
coutGetline (fin, STR);
}
3,
While (getline (fin, STR))
{
cout}
Or is there any other way?

CodePudding user response:

Other writing, for example, while true cycle, abnormal or eof will break, etc.
But usually use is your list of 3
And you listed 2 May not correct, general need not

CodePudding user response:

2 if the last line not line/enter, if end of file directly, can read less line

CodePudding user response:

reference 1st floor qybao response:
other writing also, such as the while true cycle, abnormal or eof will break, etc.
But usually use is your list of 3
2 has listed and you may not be correct, general need not


refer to the second floor early play big play nuclear response:
2 if the last line of no/enter a new line, end of file directly, can read a line less

Remember in my impression, the last line if there is one data read out, is not eof (), and then have the end of the file to read again, would eof ()?
Remember when I used to learn C language EOF character is such,

CodePudding user response:

Is really read the last line (even if there is no line/enter), but the logic of 2 last line there is no output if not wrap/enter

CodePudding user response:

references 4 floor early play play nuclear response:
is really read the last line (even if there is no line/enter), but the logic of 2 last line there is no output if not wrap/enter

I use the Code: : Blocks tried once, 1 write getline seems right,
Similar wording, the following code seems to have a problem:
# include & lt; Iostream>
# include & lt; Fstream>
# include & lt; String>

using namespace std;

Int main ()
{
Ifstream fin (" b.t xt ");//b.t xt lines end with a newline: ABC hello world
if(! Fin. Is_open ())
{
cout<& lt;" File does not exist "& lt; return 1;
}
String s;
//while (fin> S)
while(! Eof ())
fin.{
Fin> s;
cout}
Fin. The close ();
return 0;
}

CodePudding user response:

Old shall have to try my fifth floor code issue?

CodePudding user response:

While (fin> S)
{
coutBreak the if (fin eof ());
}
Writing, the last line have line/return can correct output

CodePudding user response:

refer to 7th floor early play big play nuclear war reply:
while (fin> S)
{
coutBreak the if (fin eof ());
}
Did like this, the last line wrap/return can correct output

If about this for a while, if that can not line,

CodePudding user response:

refer to the eighth floor ooolinux response:
Quote: refer to 7th floor early play big play nuclear war reply:
while (fin> S)
{
coutBreak the if (fin eof ());
}
Did like this, the last line wrap/return can correct output

If this for a while, if that can not line,

If don't result also appeared to be correct,

CodePudding user response:

I have seen the while (fin> A> B) the attached to write can also, and more convenient, but recently I have a homemade STL, the second edition of the code all are similar to the code and the first floor of my first writing, feel eof so not by the spectrum,
  • Related