• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

using fstream to save files does not save "\r" and "\n"

i'm trying to write a program thats similar to a notepad
when i save a string with characters such as "\r" and "\n" somehow it will get ignored, like if i save a string like this "abcde\rfg", when i view it with notepad it will apear as just abcdefg and not 2 lines
can anyone help me out?

fstream thefile;
thefile.open("OurText.txt", ios::out | ios::binary);
                        
char abc[] = "adsw21\nadwjdaw";

thefile << abc;
                        
thefile.close();
0
madonion87
Asked:
madonion87
  • 2
1 Solution
 
grg99Commented:
The concept of end-of-line is rather loosely defined in the Windows world.  
Traditionally it's CR-LF  "\r\n", but with all the C code getting ported, there's the compatability kludge in the i/o library that if you open the file in non-binary mode the read/write code does the mapping for you.  So try opening the file in text mode and the \n's will get converted (usually).

0
 
SteHCommented:
I don't agree that end-of-line is loosely defined. For DOS/Windows based PCs it is "\r\n"! Several editors can open files and if tehy seem to be text they automatically translate single '\r' or '\n' encountered to end-of-line chars. So either go with grg99's comment and write text files or assure that you use "\r\n" on windows as end-of-line marker.
0
 
SteHCommented:
Sorry wanted to add that Notepad is not one of those smart editors so it won't recognize '\r' as line termination.
0
 
madonion87Author Commented:
yea got it
this is me testing for another problem though, will post in another question
thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now