-1

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)

4

2 回答 2

0

你为什么不测试它?

    > cat temp.cc
    #include<iostream>

using namespace std;

int main()
{

string str="vijay\n";

cout << str <<"this is the next line"<<endl;

return 0;
}

    > ./a.out
    vijay
    this is the next line
    > 
于 2012-07-24T06:24:10.687 回答
0

答案是肯定的。当然,字符串将存储传递给它的字符数据。您的代码中可能有一些格式化操作正在占用空格或换行符。

于 2012-07-24T04:41:00.440 回答