Reading File and print on the screen

Aug 5, 2011 at 5:14pm
Hey Guys;
I just saw a strange thing in my codes in reading file from a text file here is codes then I explain where is the strange thing...

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
#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;

int main(){
	char* name;
	int num;
	string s ;
	name = new char[20];
	ifstream fin;
	fin.open("students.dat",ios::in);
	if(!fin.good())
		cout<<"problem\n";
	else
		cout<<"fine";

	fin>>num;     \\first line in the file is number of lines
	cout<<num<<"number of record";

	while(!fin.eof()){
		cout<<"miracle";
		getline(fin,s);
		cout<<s;
       }
	return 0;
}


if i put cin.ignore(); before getline the out put wait for the enter and after entering appear....On the other hand, if i put \n in the end of cout<<"miracle"; half of the things come out....why?


thanks any one if explain what's happening when file is reading and the enter and cin.ingnore() what are doing over there?!....

cheers
Aug 6, 2011 at 5:55am
ُThanks to myself for finding the answer, I just wrote cout<<s<<endl; in line 24 instead of cout<<s;

exactly I don't know what it does but the program works fine and I know it does something with \n or the output having problem with its buffer.
Last edited on Aug 6, 2011 at 5:56am
Aug 6, 2011 at 3:39pm
From MSDN - std::endl

Terminates a line and flushes the buffer.


Topic archived. No new replies allowed.