C++从纯文本中读取字符串(string)内容


C++从纯文本中读取字符串(string)内容

void demo::fr()
{
    // read string from text file
    ifstream fin;
    fin.open("sample.in");
    ifstream fin_result;
    fin_result.open("sample.out");

    string line;
    string line_result;
    int no=0;
    if(NULL!=getline(fin,line))
        {
            cout<<"line no="<<line<<endl;
        }
    while(getline(fin,line)&&getline(fin_result,line_result))
        {
            cout<<"no"<<no<<":"<<line.size()<<":"<<line<<endl;
            line_result = line_result.substr(9);  // read the content from the 9 position
            cout<<"no"<<no++<<":"<<line_result.size()<<":"<<line_result<<endl;
        }
    string ss("god!");    // show the length of string ss
    cout<<"no="<<ss.size()<<" content:"<<ss<<endl;
    fin.close();
 fin_result.close();
}

相关内容