C ++将文件读入多个数组
C++ reading file into multiple arrays
我正在尝试将文本文档中的文件读入 2 个数组。我已经排除了问题出在我的其他功能或我的主电源上,剩下大约 5 行代码....
它会循环遍历我的文档直到最后,但它只循环输入一次 txt。任何想法都会很棒!
void load_donations(string donor[], string donation[])
{
string text;
cout << "What *.txt file would you like to load? ";
cin >> text;
text += ".txt";
cout << text << endl;
ifstream infile;
infile.open (text.c_str());
int i = 0; //moves to next slot in array
while (!infile.eof())
{
getline(infile, donor[i]);
getline(infile, donation[i]);
i++;
}
infile.close();
}
为此,您需要声明动态字符数组,并且
然后逐个字符地从文件中读取并将该字符保存在循环内的动态数组中。
这对你有意义吗?
问题是我没有将我的计数从这个函数传递给打印出我的数组的函数。我只是在像 Peter 建议的那样创建一个最小值时才注意到它。在我出去的路上随意踢我。
我正在尝试将文本文档中的文件读入 2 个数组。我已经排除了问题出在我的其他功能或我的主电源上,剩下大约 5 行代码....
它会循环遍历我的文档直到最后,但它只循环输入一次 txt。任何想法都会很棒!
void load_donations(string donor[], string donation[])
{
string text;
cout << "What *.txt file would you like to load? ";
cin >> text;
text += ".txt";
cout << text << endl;
ifstream infile;
infile.open (text.c_str());
int i = 0; //moves to next slot in array
while (!infile.eof())
{
getline(infile, donor[i]);
getline(infile, donation[i]);
i++;
}
infile.close();
}
为此,您需要声明动态字符数组,并且 然后逐个字符地从文件中读取并将该字符保存在循环内的动态数组中。
这对你有意义吗?
问题是我没有将我的计数从这个函数传递给打印出我的数组的函数。我只是在像 Peter 建议的那样创建一个最小值时才注意到它。在我出去的路上随意踢我。