Can a string contain a newline character if it is given from a character type?

advertisements

What I mean is this. Say we read from standard input a bunch of text, and one by one we read it into a character type. If one of these characters is a newline, obviously this variable can hold information about the new line. What if I push this character into a string stream, and then output the contents of the string stream into a string?

It appears that this new string doesn't hold any data about the newline character.

Is there anyway to have the string keep this information?

Code snippet:

    stringstream ssChar;
    unsigned char aChar;
    string strChar;

    sourceFile >> noskipws >> aChar;
    ssChar << aChar;
    getline(ssChar, strChar);
    //ssChar.str("");
    //ssChar.seekg(0);
    cout << "Next char is: " << (int)aChar << endl;
    cout << "Length of char(from stringstream): " << strChar.length() << endl;

Input: file with a newline xxd sourceFile

0000000: 0a0a (2 newlines actually) ..

Output: Next char is: 10 (ascii newline) Length of char: 0 (str is empty however)


The answer is yes. Of course the string will store character data passed into it. There is probably some formatting operation in your code which is eating whitespace or newlines.