拆分来自文件的一行。 (部分需要包含空格)c++
Split up a line coming in from a file. (part needs to include whitespace) c++
我需要拆分来自文件的一行。该文件包含带有 ID、作者、出版日期和书名的图书列表。
91507 刘易斯卡罗尔 1865 爱丽丝梦游仙境
我可以把作者名分成名字和姓氏,但是标题呢??我不知道该如何处理。我应该只创建第二个文件以便使用 getline 吗?
我是新手,我不明白如何为此使用 stringsteam,或 split() 或类似的东西,所以如果这是你的建议,你能解释一下发生了什么吗?我没有充分理解我看过的任何示例,无法对其进行修改以适合我的目的。
这样做:
#include <fstream>
using namespace std;
int main()
{
ifstream fin("somefile.txt");
string id = "";
string authFirst = "";
string authLast = "";
string date = "";
string title = "";
string temp = "";
fin >> id >> authFirst >> authLast >> date >> title;
getline(fin, temp);
title += temp;
cout << title << endl;
return 0;
}
ifstream fin;
fin.open(filename)
string id;
string first;
string last;
string year;
string title;
if(fin.is_open())
{
while(!fin.eof())
{
fin >> id >> first >> last >> year;
getline(fin, title);
cout << first << " " << last << " " << title << endl;
}
fin.close();
}
我需要拆分来自文件的一行。该文件包含带有 ID、作者、出版日期和书名的图书列表。
91507 刘易斯卡罗尔 1865 爱丽丝梦游仙境
我可以把作者名分成名字和姓氏,但是标题呢??我不知道该如何处理。我应该只创建第二个文件以便使用 getline 吗?
我是新手,我不明白如何为此使用 stringsteam,或 split() 或类似的东西,所以如果这是你的建议,你能解释一下发生了什么吗?我没有充分理解我看过的任何示例,无法对其进行修改以适合我的目的。
这样做:
#include <fstream>
using namespace std;
int main()
{
ifstream fin("somefile.txt");
string id = "";
string authFirst = "";
string authLast = "";
string date = "";
string title = "";
string temp = "";
fin >> id >> authFirst >> authLast >> date >> title;
getline(fin, temp);
title += temp;
cout << title << endl;
return 0;
}
ifstream fin;
fin.open(filename)
string id;
string first;
string last;
string year;
string title;
if(fin.is_open())
{
while(!fin.eof())
{
fin >> id >> first >> last >> year;
getline(fin, title);
cout << first << " " << last << " " << title << endl;
}
fin.close();
}