【问题标题】:Assign file text into array将文件文本分配到数组中
【发布时间】:2015-05-27 14:00:56
【问题描述】:

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

username1
password1
username2
password2
username3
password3
username4
password4

如何将用户名2 和密码2 解析成数组?我知道我可以在这种情况下使用 struct,但不允许我使用 struct 进行分配。

谢谢。

【问题讨论】:

标签: c++ arrays file


【解决方案1】:

例子:

#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
}

【讨论】:

  • A) 你做他的作业是对他的伤害,B) 他的任务是使用 2 个数组而不是地图。
猜你喜欢
  • 2012-10-09
  • 1970-01-01
  • 2018-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-20
  • 2014-12-30
  • 2014-11-17
相关资源
最近更新 更多