how to detect will ofstream use \r symbol on writing

I'd like to know will ofstream print "\r" symbol on the end of each line (windows/unix systems is possible).

is it possible to say not to use "\r"?

It is better to get that from ofstream, or something like that (boost (filesystem) can be solution too).
I don't think I understood your question. If you could explain (in detail) I may be able to help you.
For instance, do you want to print "\r" (exactly) in your file, or are you talking about carriage return? what do you want to know?
1) I'd like to understand will ofstream print "\r" into each line of the file.
2) I'd like to understand is it possible to control printing "\r".

somethink like:

ofstream out_file("file_name","a");
bool label = false;
if (out_file.will_print_r() )
{
label = true;
}
out_file << "string" << std::endl;
out_file.close();

// if (label == true) file size == 8;
// if (label == false) file size == 7;

There is no portable way to find out if the line terminator '\n' in text mode corresponds to one or multiple characters in the file.

You can guarantee that no \r is added if you open your file with ios::binary
Last edited on
Seems to me that it does append a newline CR+LF at the end of a string. So, you basically want to get rid of Carriage return? Well I don't know, but I think this program might help you.. try it
But I honestly admit that I don't know why it works,, at first I thought that binary mode might help so I just added the ios::binary, but I was expecting both CR+LF to be out of file, but though CR was absent, LF wasn't.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream out_file("something.txt",ios::out|ios::binary);      //This results in 7 bytes
                                                                                     //Omitting ios::binary results in 8 bytes 
bool label = false;
char ar[]="string";
out_file << ar << std::endl;
out_file.close();
}
You must use write() member function, at least I don't have problems with writing binary files this way.
As Cubbi says, you can use >> in binary mode and it will not add \r.
Topic archived. No new replies allowed.