#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int a , b , c , d;
ifstream myfile;
myfile.open ("numbers.txt");
myfile >> a, b, c;
d = a + b + c;
ofstream myfile;
myfile.open ("result.txt");
myfile << d;
myfile.close();
return 0
}
number.txt
文件包含 3 个数字 10
, 8
, 9
。我试图让程序读取它们并将它们汇总到 results.txt 中。
我得到的错误是:
conflicting declaration 'std :: ifstream myfile'
no match for 'operator << in myfile << d'
'myfile' has a previous declaration as 'std :: ifstream myfile'
最佳答案
(这只解决了您代码中的两个错误之一。)
myfile >> a, b, c;
此行不会读取所有三个变量 a
、b
和 c
的输入。它只读取 a
的输入,然后计算 b
并丢弃该值,然后计算 c
并丢弃该值。
你想要的是:
myfile >> a >> b >> c;
这将从 myfile
中读取所有三个变量的值。
关于C++ 带文件的输入/输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30501331/