Big problem in reading line

Hi ,i have a file his name is (textfile.txt) in this file a 3 sentences which at t he end of every sentence i have the sign(#) , i want when i reach the sign to stop read from that line and go to below line i tried but it wont come true it reads only one line

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 #include<iostream>
#include<fstream>
using namespace std;
int main()
{
	ifstream infile;
	infile.open("textfile.txt");
	ofstream outfile;
	char x;
	char y='#';
	int upcounter=0;
	int linecounter=0;
	
	infile>>x;
	
	while(x!=y){
		cout<<x<<endl;
		infile>>x;
	}
	

	system("pause");
	return 0;
}
why you need to do so? use getline function and get rid of you sign

Lets say that you have your txt file

textfile.txt
first line
second line
third line


It is easy to achieve what you asking for simply with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# include <iostream>
# include <fstream>
using namespace std;

int main()
{
    ifstream infile("textfile.txt",ios::in);

	if(infile)
	{
	    while(infile)
	    {
	        char line[200];

	        infile.getline(line,200);

	        cout << line << endl;
	    }
	}else cout << "Cannot open this file" << endl;

    return 0;
}


If you incest using your methode just call

Hope thats helps
i want to ask you about why you used char line[200]
why not using char line !!??
well if you turn it to char line you'll get an error because the prototype of istream::getline is istream& getline (char* s, streamsize n );

char line ,is only one character
char line[200] ,is an array of 200 characters

thanks alot
Topic archived. No new replies allowed.