将文件文本分配到数组中

时间:2015-05-27 14:00:57

标签: c++ arrays file

我有一个问题,如何将数据分配到数组中?例如,我有一个文本文件,其中包括:

username1
password1
username2
password2
username3
password3
username4
password4

如何将username2和密码2解析为数组?我知道我可以在这个场景中使用struct但是我不允许在我的作业中使用struct。

感谢。

1 个答案:

答案 0 :(得分:0)

示例:

#include <fstream>

ifstream infile;
infile.open("filename");

std::string a;
std::string b;
std::map<std::string, std::string>myMap;
std::string arr[10000];
int count = 0;
while (infile >> a >> b)
{
    arr[count++] = a; //username
    arr[count++] = b; //password
}