关于getline()的一片文章(zt)
2007-06-06 21:40
302 查看
怎样获得文本的每一行呢 标准库支持 getline()函数 声明如下
istream&
getline( istream &is, string str, char delimiter );
getline()读取 istream 对象 向 string 对象插入字符 包括空格 直到遇到分割符 文件
结束 或者被读入的字符序列等于 string 对象的 max_size()值 在该点处读入操作失败。
在每次调用 getline()之后 我们都会将 str 插入到代表文本的字符串 vector 中 下面是一
般化的实现17 我们已经将它提取到一个函数中 命名为 retrieve_text()
// 返回值是指向 string vector 的指针
vector<string,allocator>*
retrieve_text()
{
string file_name;
cout << "please enter file name: ";
cin >> file_name;
// 打开文本文件以便输入 ...
ifstream infile( file_name.c_str(), ios::in );
if ( ! infile ) {
cerr << "oops! unable to open file "
<< file_name << " -- bailing out!/n";
exit( -1 );
}
else cout << '/n';
vector<string, allocator> *lines_of_text =
new vector<string,allocator>;
string textline;
typedef pair<string::size_type, int> stats;
stats maxline;
int linenum = 0;
while ( getline( infile, textline, '/n' )) {
cout << "line read: " << textline << '/n';
if ( maxline.first << textline.size() ) {
maxline.first = textline.size();
maxline.second = linenum;
}
lines_of_text->push_back( textline );
linenum++;
}
return lines_of_text;
}
istream&
getline( istream &is, string str, char delimiter );
getline()读取 istream 对象 向 string 对象插入字符 包括空格 直到遇到分割符 文件
结束 或者被读入的字符序列等于 string 对象的 max_size()值 在该点处读入操作失败。
在每次调用 getline()之后 我们都会将 str 插入到代表文本的字符串 vector 中 下面是一
般化的实现17 我们已经将它提取到一个函数中 命名为 retrieve_text()
// 返回值是指向 string vector 的指针
vector<string,allocator>*
retrieve_text()
{
string file_name;
cout << "please enter file name: ";
cin >> file_name;
// 打开文本文件以便输入 ...
ifstream infile( file_name.c_str(), ios::in );
if ( ! infile ) {
cerr << "oops! unable to open file "
<< file_name << " -- bailing out!/n";
exit( -1 );
}
else cout << '/n';
vector<string, allocator> *lines_of_text =
new vector<string,allocator>;
string textline;
typedef pair<string::size_type, int> stats;
stats maxline;
int linenum = 0;
while ( getline( infile, textline, '/n' )) {
cout << "line read: " << textline << '/n';
if ( maxline.first << textline.size() ) {
maxline.first = textline.size();
maxline.second = linenum;
}
lines_of_text->push_back( textline );
linenum++;
}
return lines_of_text;
}
相关文章推荐
- 关于职业规划的一片好文章
- 闲人博上关于R语言介绍的一片好文章
- 推荐一片关于Testing的文章
- 关于PropertyGrid的使用以及控件编写(转载微软的一片文章)
- 分享一片关于Android开发的文章
- 一片很好的关于感情的文章
- 推荐一片关于rrdtool的好文章,写的很细
- 关于Page Life Expectancy的一片文章
- 关于一片如何学习好linux的文章,感觉不错就给转过来了。
- 关于在 Linux 下多个不相干的进程互斥访问同一片共享内存的问题,记录锁,好文章
- 关于字符集(讲的比较清楚的一片字符集科普文章)
- MSDN上的一片关于observer模式的文章
- zt最专业的关于ACCESS数据库的文章!
- 一片很好的关于感情的文章
- 关于hibernate date 检索的一片文章
- 一片关于懒加载不错的文章
- 关于android HAL接口的一片比较好的文章
- 看到《新发现》上面一片关于心理学的文章。
- 一片很全的关于angularjs的文章
- 转贴一片关于Web2.0的文章