处理从一个文本文件到另一个的文本行 [英] Processing lines from one text file to another

查看:213
本文介绍了处理从一个文本文件到另一个的文本行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要感谢所有帮助我处理我的问题的人。我终于能够按出生年份安排数据!非常感谢。

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 

如何将这些数据输出到一个新的文本文件,但只列出三个东西:year-name - 时间...,以使这两行如下所示:

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 << '\n';

但我有一种感觉,我不能循环遍历整个文本文件和所有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个字符

"I know that the year is always the 54th to 58th character in each line"

因此,请考虑:

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 << "\n";

    in.close();
    out.close();
}

这篇关于处理从一个文本文件到另一个的文本行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆