mercredi 1 mars 2017

Why istream::rdbuf() append new line symbol "\n" even if the input file is just one line string in C++?

Suppose a file "a.txt" which contains just one line string as below:

hello world

I don't contain any new line symbol like "\n".

void loadFileStr(istream& stream, stringstream& strs){
     str << stream.rdbuf();
}

I call loadFileStr and output its read string.

int main(){
    stringstream strs;
    ifstream ifs("a.txt",strs);
    loadFileStr(ifs, strs)
    ifs.close();
    std::cout << strs.str(); 
}

However,

strs.str()

shows hello world with newline.

hello world
'\n'

(\n) is just a space actually.

Is this rdbuf() feature or how can i read this file without any "\n"?

Aucun commentaire:

Enregistrer un commentaire