使用git bash无法执行包含fstream命令的C++目标程序,而在cmd中可以执行是什么情况?

新手上路,请多包涵

开始使用vscode作为常用编辑器,听说git bash作为程序终端十分不存,但最近在调试C++时遇到了问题:
在编译后调试如下程序时(使用"./program.exe"命令)

#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
int main()
{
  char buffer[256];
  ifstream in("test.txt");
  if (!in.is_open())
  {
cout << "Error opening file";
exit(1);
  }
  while (!in.eof())
  {
in.getline(buffer, 100);
cout << buffer << endl;
  }
  return 0;
}

起初我以为是程序的问题,但在如下另代码中同样失效

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
  fstream f("D:\\6.Code\\CPP\\arrary_ah\\test.txt", fstream::in | fstream::out);
  cout << "hello" << endl;
  return 0;
}

但以上程序在cmd中直接"program"或"program.exe"可以执行,于是排除vscode和程序错误的问题,确定是git bash的问题,但将有关fstream的命令去掉却能正常执行.

谷歌半天没有相关结果,想请了解的或愿意帮忙的能帮我解答或复现一下吗?如果这是git bash的问题那么有什么适合vscode的其他终端吗?(不是很想用cmd或powershell)

阅读 3.7k
2 个回答

试了下在git bash下跑是会有点问题 ifstream的eof标志位没设置上 要做额外的判断

while (!in.eof() && in.tellg() != -1) 

不知道是说git bash的锅还是说mingw libstdc++的锅~~

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题