歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++從純文本中讀取字符串(string)內容

C++從純文本中讀取字符串(string)內容

日期:2017/3/1 9:59:36   编辑:Linux編程

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();
}

Copyright © Linux教程網 All Rights Reserved