Reputation: 100
And i know that this question is already asked by some other user. Yes it is duplicated and here is the link1 and link2 . I tried every solutions given in the link but still its not working. I am using TurboC++ 4.5 compiler from borland . And here is my code
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
int main()
{
ofstream fout;
fout.open("COUNTRY");
fout<<"UNITED STATES OF AMERICA \n";
fout<<"UNITED KINGDOM \n";
fout<<"SOUTH KOREA \n";
fout.close();
fout.open("CAPITAL");
fout<<"WASHINGTON \n";
fout<<"LONDON \n";
fout<<"SEOUL \n";
fout.close();
const int N=80;
char line[N];
ifstream fin;
fin.open("COUNTRY");
cout<<"\nCONTENTS OF COUNTRY FILE\n";
while(fin)
{
fin.getline(line,N);
cout<<line;
}
fin.close();
fin.open("CAPITAL");
cout<<"\nCONTENTS OF CAPITAL FILE\n";
while(fin)
{
fin.getline(line,N);
cout<<line;
}
fin.close();
return 0;
}
I checked for more solutions but can't resolve this issue and that's why i am posting it here.
Upvotes: 0
Views: 2587
Reputation: 130
you can also use 'endl' instead of '\n'
check this link: C++: "std::endl" vs "\n"
Upvotes: 1
Reputation: 4468
You need to add a newline character when you write your data out. Reading it in with getline
discards the "\n" at the end of each line.
If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it.
Here is the page that the preceding quote is from.
So the problem is that you are storing null-terminated strings that do not end with "\n".
Upvotes: 3