I am trying to read texts in a file line by line.
Example:
Line 1: Text1\nText2
here I am readingh this line using fgets/getline for 255 character.
But in the result I am getting an extra "\" appended for each "\"
so the line which I am reading above is coming as
Text1\\nTExt2.
I am now printing this text which is coming as Text1\\nTExt2 instead of
Text1
Text2
Hwo can I avoid this extra "\" coming while reding the text.
Code snap:
ifstream in(fileName);
if (!in)
{
cout << "Cannot open input file.\n";
}
char str[255];
while (in)
{
in.getline(str, 255); // delim defaults to '\n'
if (in) cout << str << endl;
}
The escape sequences like \n only apply in string literals (strings in quotes within your program). When you read a line from a file, you get exactly what's there. So if the file contains a backslash followed by the letter 'n' then the string that you read will contain a backslash followed by the letter 'n', not a single newline character.