/*
* 从文件中读取strings
* 对于文件要查找的文件,应将其放在可执行程序的或工程文件所在的目录中,否则,必须提供完整的目录;
*/
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
int main()
{
using namespace std;
ifstream fin;
fin.open("tobuy.txt");
if(fin.is_open() == false)
{
cerr << "Can't open file. Bye.\n";
exit(EXIT_FAILURE);
}
string item;
int count = 0;
// getline()有三个参数,第二个参数是本次读取的最大字符个数,第三个参数是分隔字符,作为读取一行结束的标志。
getline(fin, item, ':');
while(fin)
{
++count;
cout << count << ": " << item << endl;
getline(fin, item, ':');
}
cout << "Done\n";
fin.close();
return 0;
}
Primer plus C++ 第十六章 string类_输入
于 2012-12-21 18:29:11 首次发布