当我想运行此程序时,它会向我显示错误“ 启动失败等 ”。此外,它表示 cout , 字符串 和 关闭 无法解决,并期望“;”在“ myfile ”之前
#include <iostream>
#include <fstream>
#include <string>
int main () {
std::string line;
std::ifstream myfile ("C:\Users\Username\Desktop\cas.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
std::cout << line << '\n';
}
std::myfile.close();
}
else cout << "Unable to open file";
return 0;
}
尝试过“构建项目”
答案 0 :(得分:1)
试试这段代码。我评论了这些变化。基本上,标准库中的所有内容都必须以std::
为前缀。
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string line;
// prefixed with std
std::ifstream myfile ("C:\\Users\\Username\\Desktop\\cas.txt");
// prefixed with std, escaped backslashes
if (myfile.is_open())
{
while (std::getline(myfile, line))
// prefixed with std
{
std::cout << line << '\n';
// prefixed with std
}
myfile.close();
}
else
std::cout << "Unable to open file";
// prefixed with std
return 0;
}