处理从一个文本文件到另一个文本文件的行 [英] Processing lines from one text file to another
问题描述
我要感谢所有帮助我解决问题的人.终于可以按出生年份排列数据了!非常感谢.如果有人能帮我解决最后一个问题,那就太好了
I would like to give thanks to everyone who helped me with my problem. I was finally able to arrange the data by birth year! Very much appreciated. If anyone can help me with this final question, that'd be great
如果我的文本文件中有如下所示的行:
If I have lines in a text file that look like this:
1 4:48:08 Orvar Steingrimsson 1979 30 - 39 ara IS200
2 4:52:25 Gudni Pall Palsson 1987 18 - 29 ara IS870
我如何将这些数据输出到一个新的文本文件中,但只列出三件事:年份 - 名称 - 时间......以便这两行看起来像这样:
How can I output this data onto a new text file but only listing three things: year - name - time ... so that these two lines would look like this:
1979 Orvar Steingrimsson 4:48:08
1987 Gudni Pall Palsson 4:52:25
我的猜测是这样的:
ifstream in("inputfile.txt");
ofstream out("outputfile.txt");
int score, year;
string name, time, group, team;
while (getline(in,str));
in >> score >> time >> name >> year >> group >> team;
//and then do something like this
out << year << name << time << '
';
但是我有一种感觉,我将无法在整个文本文件和所有 200 行中循环播放.任何提示表示赞赏!谢谢
However I have a feeling I won't be able to loop this through the whole text file and all 200 lines. Any tips appreciated! Thanks
推荐答案
我知道年份总是每行的第 54 到 58 个字符"
所以,考虑一下:
int main ()
{
ifstream in("laugavegurinn.txt", ios::in);
ofstream out("laugavegurinn2.txt");
string str;
multimap<int, string> datayear;
while (getline(in,str)) {
int year = stoi(str.substr(54, 4));
datayear.insert(make_pair(year,str)); //use insert function
// multimap don't have [] operator
}
for (auto v : datayear)
out << v.second << "
";
in.close();
out.close();
}
这篇关于处理从一个文本文件到另一个文本文件的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!