Wrong output displayed

Aug 9, 2013 at 3:38pm
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;
	}

}


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