Wrong output displayed

Im supposed to be getting the following output:

-- Write that should fail --
The filename is blank or the file could not be opened so the write/append operation could not be completed.
-- Write + Read --
This is the first line. It should disappear by the end of the program.
-- Write + Read --
This is another line. It should remain after the append call.
This call has two lines.
-- Append + Read --
This is another line. It should remain after the append call.
This call has two lines.
This is the final line in the text.txt file. It should come after the line written
by the second write.
-- Testing Nonexistant File Read --
Fitchfork marks for you!


but i get this output:

-- Write that should fail --
The filename is blank or the file could not be opened so the write/append operation could not be completed.
-- Write + Read --
This is the first line. It should disappear by the end of the program.-- Write + Read --
This is another line. It should remain after the append call.This call has two lines.-- Append + Read --
This is another line. It should remain after the append call.This call has two lines.This is the final line in the text.txt file. It should come after the line written by the second write.-- Testing Nonexistant File Read --
Fitchfork marks for you!


this is the code i used
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
string FileInteraction::read() const
{
	string fileContent, content;
	ifstream file;
	file.open(filename.c_str(), ios::in);
	
	getline(file, fileContent);
	content += fileContent;
	while(file)
	{		
		
		if(file.good())
		{
			getline(file, fileContent);
			if(fileContent == "\n")
			{
				cout << endl;
			}
			content += fileContent;
		}
	
	}
	return content;
        //this is for if it cant read from the file for any reason
	if(file.bad())
	{
		fileContent = "";
		return fileContent;
	}

}


Try doing if(fileContent == '\n' || fileContent == '\r')
ok tanx i tried it and it didnt work
Last edited on
Maybe try putting a newline after every fileline?
change content += fileContent; to content += fileContent + endl;
and change cout << endl; to content += endl;
Ok it worked thank you soo much for your help :-)
Topic archived. No new replies allowed.