2013-09-27 27 views
0

我有200行,看起来像这样的文本文件:重新排列线列在文本文件

1 4:48:08 Orvar Steingrimsson     1979 30 - 39 ara  IS200 
2 4:52:25 Gudni Pall Palsson     1987 18 - 29 ara  IS870 

我的代码排序这些行每个选手,而不是因为它在时间的出生年份上面的例子。所以,现在的行排序为这样的:(只多不少行)

2 4:52:25 Gudni Pall Palsson     1987 18 - 29 ara  IS870 
1 4:48:08 Orvar Steingrimsson     1979 30 - 39 ara  IS200 

我的问题是我怎么能现在做上市的三件事情:年 - 名字 - 时间......所以,这两个行会看像这样:

1987 Gudni Pall Palsson 4:52:25 
1979 Orvar Steingrimsson 4:48:08 

我的代码,到目前为止,这种种按照正确的顺序行:

#include <iostream> //for basic functions 
#include <fstream> //for basic file operations 
#include <string> //for string operations 
#include <map> //for multimap functions 

using namespace std; 

int main() 
{ 
ifstream in("laugavegurinn.txt", ios::in); 
ofstream out("laugavegurinn2.txt"); 
string str; 
multimap<int, string> datayear; //creates multimap linking each line to year 

in.ignore(80);//ignores header of the file - 80 characters - to escape problems with while loop 

// while (getline(in,str)) { 
// string name = str.substr(18,30); 
    // string time = str.substr(8,7); 

//} 


while (getline(in,str)) { 
    int year = stoi(str.substr(54, 4)); 
    datayear.insert(make_pair(year,str)); //insert function and pairs year and string 
} 

for (auto v : datayear) 
    out << v.second << "\n"; 

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

我不能创建变量下的名称和时间,然后将它们添加到线:出<< v.first <<名字<<时间<<“\ n \; –

+0

什么应是第三个版本这个问题?首先是[这](http://stackoverflow.com/q/19055100/1870232)。我想知道这是一个[XY](http://meta.stackexchange.com/q/66377/ 232397)问题? – P0W

+0

如果所有行按相同顺序排列,则可以使用#include stringstream获取每个字符串的单个信息使用某些std :: vector构造对所有信息进行排列,然后您只需要打印我对你的问题是否正确? – Martin

回答

1

我建议你使用一个类或结构与一个成员代表表中的每一列。一个实例代表一行。

从这个类中,您可以编写方法以任意顺序显示数据。

您可以编写排序类来按任何成员排序对象。

搜索StackOverflow以获取从文件中读取数据的示例。

1

您正在将每行视为单个字符串:重新排列字段的唯一方法是分别提取每个字段和输出。我的例子使用一个结构与您要输出的字段。

#include <iostream> //for basic functions 
#include <fstream> //for basic file operations 
#include <string> //for string operations 
#include <map> //for multimap functions 

using namespace std; 

typedef struct { 
    int year; 
    string name; 
    string time; 
} Fields; 

int main() 
{ 
    ifstream in("names.txt", ios::in); 
    ofstream out("namesout.txt"); 
    string str; 
    multimap<int, Fields> data; //creates multimap linking each line to year 

    in.ignore(80); 

    Fields tempField; 

    while (getline(in,str)) { 
    tempField.year = stoi(str.substr(51, 4)); 
    tempField.name= str.substr(15, 30); 
    tempField.time= str.substr(5, 8); 
    data.insert(make_pair(tempField.year,tempField)); //insert function and pairs year and string 
    } 

    for (auto v : data) 
    out << v.second.year << " " << v.second.name << " " << v.second.time<< "\n"; 

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

有没有真正简单的方法来做到这一点。我认为你应该做的是每次读一行,并将它们解析成某种结构。然后根据需要显示/写入适当的项目。

棘手的部分是当你试图解析人的名字。由于一个人的名字可能由多个字符串组成。从而使名称部分的识别变得困难。

vinaut所说的几乎相同。

0
$ cat file 
2 4:52:25 Gudni Pall Palsson     1987 18 - 29 ara  IS870 
1 4:48:08 Orvar Steingrimsson     1979 30 - 39 ara  IS200 

$ sed -r 's/[[:digit:]]+[[:space:]]+([[:digit:]:]+)[[:space:]]+(.*[^[:space:]])[[:space:]]+([[:digit:]]{4})[[:space:]].*/\3 \2 \1/' file 
1987 Gudni Pall Palsson 4:52:25 
1979 Orvar Steingrimsson 4:48:08