聂嘉辉 发表于 2021-7-14 19:41:11

新手问题,求c++高手指教

为什么这段代码没有输出?
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::ifstream fin("my.txt");
    if(!fin.is_open())
      std::cout << "open error";
      return 1;
    std::string temp;
    fin >> temp;
    fin.close();
    std::cout << '*' << temp << '*';
}
my.txt
This is a test file.
You can write something anywhere.
It has 70 chars.
It has 4 lines.

人造人 发表于 2021-7-14 19:45:27

我粘贴了一下,然后代码就变成了这样
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::ifstream fin("my.txt");
    if(!fin.is_open())
      std::cout << "open error";
    return 1;
    std::string temp;
    fin >> temp;
    fin.close();
    std::cout << '*' << temp << '*';
}

聂嘉辉 发表于 2021-7-14 19:50:54

找到原因了

聂嘉辉 发表于 2021-7-14 19:51:49

if(!fin.is_open())
{
std::cout << "open error";
return 1;
}
要加个括号
页: [1]
查看完整版本: 新手问题,求c++高手指教