C++ C++;编译错误 #包括 #包括 #包括 使用名称空间std; int main() { ifstream输入文件; 字符串示例; 整数, 总数=0; //提示用户输入要打开的文件的名称 举个例子; //打开分配给变量“example”的输入文件 open(例如.c_str()); //如果文件已成功打开,请对其进行处理 如果(输入文件) { //循环直到达到EOF while(inputFile>>numbers)//如果读取了值 { 总数+=个数; } //关闭文件 inputFile.close(例如.c_str()); cout
C++ C++;编译错误 #包括 #包括 #包括 使用名称空间std; int main() { ifstream输入文件; 字符串示例; 整数, 总数=0; //提示用户输入要打开的文件的名称 举个例子; //打开分配给变量“example”的输入文件 open(例如.c_str()); //如果文件已成功打开,请对其进行处理 如果(输入文件) { //循环直到达到EOF while(inputFile>>numbers)//如果读取了值 { 总数+=个数; } //关闭文件 inputFile.close(例如.c_str()); cout,c++,compiler-errors,fstream,ifstream,C++,Compiler Errors,Fstream,Ifstream,ifstream::close不接受任何参数 将第36行更改为inputFile.close();删除example.c.str() 正确的状态为: #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream inputFile; string example; int numbers,
ifstream::close
不接受任何参数
将第36行更改为inputFile.close();
删除example.c.str()
正确的状态为:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inputFile;
string example;
int numbers,
totalNumbers = 0;
// Prompt user to input name of the file to open
cout << "Please enter the name of the program" << endl;
cin >> example;
// Open the input file that is assigned to the variable 'example'
inputFile.open(example.c_str());
// If the file successfully opened, process it
if(inputFile)
{
// Loop until the EOF is reached
while(inputFile >> numbers) // If a value was read
{
totalNumbers += numbers;
}
// Close the file
inputFile.close(example.c_str());
cout << totalNumbers;
}
else
{
// Display an error message
cout << "could not access file";
}
return 0;
}
我们不需要传递参数阅读错误消息,问题已经说得很清楚了。谢谢。这是一个简单的错误。你接受的答案与之后发布的答案完全相同,甚至不向上投票。进行向下投票。你不需要关闭该文件。析构函数会这样做。
inputFile.close();