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

Posted on 2004-11-12
Last Modified: 2006-11-17
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;"OurText.txt", ios::out | ios::binary);
char abc[] = "adsw21\nadwjdaw";

thefile << abc;
Question by:madonion87
    LVL 22

    Accepted Solution

    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).

    LVL 13

    Expert Comment

    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.
    LVL 13

    Expert Comment

    Sorry wanted to add that Notepad is not one of those smart editors so it won't recognize '\r' as line termination.

    Author Comment

    yea got it
    this is me testing for another problem though, will post in another question

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now