0

ストリームからいくつかのデータを出力する必要があります - istringstream ( in main () )。

例:

void Add ( istream & is )
{
    string name;
    string surname;
    int data;

    while ( //something )
    {
        // Here I need parse stream

        cout << name;
        cout << surname;
        cout << data;
        cout << endl;
    }

}

int main ( void )
{
    is . clear ();
    is . str ( "John;Malkovich,10\nAnastacia;Volivach,30\nJohn;Brown,60\nJames;Bond,30\n" );
    a . Add ( is );
    return 0;
}

この行を解析する方法

is.str ("John;Malkovich,10\nAnastacia;Volivach,30\nJohn;Brown,60\nJames;Bond,30\n");" 

name;surname,data

4

2 に答える 2

1

これはやや壊れやすいですが、フォーマットが投稿したものとまったく同じであることがわかっている場合は、何も問題はありません。

while(getline(is, name, ';') && getline(is, surname, ',') && is >> data)
{
    is.ignore();    //  ignore the new line
    /* ... */
}
于 2013-05-16T18:42:02.297 に答える
0

区切り文字が常に;and,になることがわかっている場合は、かなり簡単です。

string record;
getline(is, record); // read one line from is

// find ; for first name
size_t semi = record.find(';');
if (semi == string::npos) {
  // not found - handle error somehow
}
name = record.substr(0, semi);

// find , for last name
size_t comma = record.find(',', semi);
if (comma == string::npos) {
  // not found - handle error somehow
}
surname = record.substr(semi + 1, comma - (semi + 1));

// convert number to int
istringstream convertor(record.substr(comma + 1));
convertor >> data;
于 2013-05-16T18:21:36.057 に答える