我需要一個解決方案,使用基於姓氏的C++對unix pwd文件進行排序。該文件的格式是用戶名,密碼,uid,gid,名稱,homedir,shell。所有分隔符都用冒號分隔。名稱字段包含姓氏後綴的名字,這兩個名字都是由空格分隔的,我可以使用地圖對值進行排序,併發布我的代碼。可以有人建議我改進,我可以做我的代碼請。此外,我無法看到我的文件中的排序行。排序文件中的字符串
string line,item;
fstream myfile("pwd.txt");
vector<string> lines;
map<string,int> lastNames;
map<string,int>::iterator it;
if(myfile.is_open())
{
char delim =':';
int count =0;
while(!myfile.eof())
{
count++;
vector<string> tokens;
getline(myfile,line);
istringstream iss(line);
lines.push_back(line);
while(getline(iss,item,delim))
{
tokens.push_back(item);
}
cout<<tokens.size()<<endl;;
size_t i =tokens[4].find(" ");
string temp = tokens[4].substr(i,(tokens[4].size()-i));
cout<<temp<<endl;
lastNames.insert(pair<string,int>(temp,count));
tokens.clear();
}
myfile.seekg(0,ios::beg);
for(it=lastNames.begin();it!=lastNames.end();it++)
{
cout << (*it).first << " => " << (*it).second << endl;
int value=lastNames[(*it).first ];
myfile<<lines[value-1]<<endl;
cout<<lines[value-1]<<endl;
cout<<value<<endl;
}
}
另外我有問題寫入文件我無法看到排序結果。
我的問題:
Can someone please explain me why I am unable to see the written results in the file!
感謝&問候,
像老鼠。
定義「非常大」。對40MB文件和4GB文件進行排序的最佳方法是非常不同的,儘管對於pwd文件來說這兩個文件都可以說是「非常大」。 – 2010-07-01 04:16:07
爲什麼你需要在C中做到這一點?命令行'sort'實用程序可以很快解決您的問題。 – 2010-07-01 04:16:24
假設該文件是在幾兆字節的順序。我也想知道如何排序它,如果它的幾個千兆字節的順序 – mousey 2010-07-01 04:17:24