【发布时间】:2019-03-08 05:35:45
【问题描述】:
对于这个任务,我打开一个文本文件并尝试将第 1 行和第 3 行读入名为 front 的数组(分别在索引 0 和 1 处),然后将第 2 行和第 4 行读入名为 back 的数组中(在索引处0 和 1),但它并没有完全做到这一点。没有任何东西输入到数组中,我的循环逻辑必须关闭。我想按原样阅读每一行(包括空格),直到换行符为止。任何帮助表示赞赏。
void initialize(string front[], string back[], ifstream &inFile)
{
string fileName = "DevDeck.txt"; //Filename
string line;
inFile.open(fileName); //Open filename
if (!inFile)
{
cout << "Couldn't open the file" << endl;
exit(1);
}
//Create the parallel arrays
while (!inFile.eof())
{
for (int index = 0; index < 4; index++)
{
getline(inFile, line);
front[index] = line; //Store in first array
getline(inFile, line);
back[index] = line; //Store in second array
}
}
}
【问题讨论】:
标签: c++ arrays loops file-io istream