我想从文件中获取数字并将它们输入到数组元素中。为什么它不起作用?while (file >> k), while (file >> m)如果我写得正确,它是如何工作的,是否可以使用不同的变量或者一个就足够了?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream file("f.txt");
ofstream fileout("g.txt");
int count = 0, m;
while (file >> m)
count++;
if (count % 2 == 1)
{
cout << "Ne 4etno. Input 4etno." << endl;
system("pause");
return 0;
}
int *a = new int[count];
int i = 0, k;
while (file >> k)
{
file >> a[i];
i++;
}
for (int j = 0; j <count; j++) {
cout << a[j] << " ";
}
file.close();
fileout.close();
system("pause");
return 0;
}
将文件倒回到开头:
注意
count。ofstream.更新
哦,如果您需要读取成对的数字,那么: