ifstream

i made example.txt file in ofstream the problem is when i enter a single letter it exits using getline but works with cin>>

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
28
29
30
31
32
33
34
35
36
37
  #include <iostream>
#include<conio.h>
#include<vector>
#include<fstream>
#include<string>
using namespace std;
int main()
{
//	ofstream file ("example.txt");
//if (file.is_open())
//{
//file << "This is a line.\n";
//file << "This is another line.\n";
//file.close();
//}
//else cout << "Unable to open file";

string line;
ifstream file("read.txt");
	if(file.is_open())
	{
while (file.good())
{
			
			/*cin>>line;*/
			getline(file,line);
			cout<<line<<endl;
}
}	
			file.close();
	

	
		cout<<"CAn not open the file \n";

	getch();
}

what is file.good in line 22 and the problem is in line 27
Last edited on
On line 19 you are using "read.txt". Is "read.txt" in the same folder as your program?
Topic archived. No new replies allowed.