打开文件 c++ 时出现问题

发布于 2024-08-03 18:48:59 字数 555 浏览 2 评论 0原文

必须是一个简单的答案,但我不知所措,这是返回错误的代码。我尝试过带或不带起始斜杠。

我不知道完整路径,我希望它是相对于exe的,这就是相对路径。我试图逃避斜线。

我的问题是当文件存在时我收到“打开文件时出错”。为什么它失败了?

  ifstream myFile("/LOGS/ex090716.txt");
  if (myFile.fail()) {cout << "Error opening file";}
  else
  {
   cout << "File opened... \n";
   //string line;
   //while( getline(myFile, line) ) {
   // cmatch results;
   // regex rx("(p|q)(=)([^ %]*)");
   // regex_search(line.c_str(), results, rx);
   // string referringWords = results[3];
   //}
   myFile.close();
  }

谢谢

Must be a simple answer but I am at a loss, here is the code that is returning an error. I have tried with and without the starting slash.

I won't know the full path, I want it to be relative from the exe, and that is the relative path. I tried escaping the slashes.

My problem is that i get "error opening file" when the file is there. why is it failing?

  ifstream myFile("/LOGS/ex090716.txt");
  if (myFile.fail()) {cout << "Error opening file";}
  else
  {
   cout << "File opened... \n";
   //string line;
   //while( getline(myFile, line) ) {
   // cmatch results;
   // regex rx("(p|q)(=)([^ %]*)");
   // regex_search(line.c_str(), results, rx);
   // string referringWords = results[3];
   //}
   myFile.close();
  }

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

乜一 2024-08-10 18:48:59

你的问题到底是什么?如果您想测试文件是否打开,请使用 is_open()

What is your problem exactly?! if you want to test if the file is open or not use is_open().

谎言月老 2024-08-10 18:48:59

去掉前导斜线

ifstream myFile("LOGS/ex090716.txt");
//...

Get rid of the leading slash

ifstream myFile("LOGS/ex090716.txt");
//...
烟燃烟灭 2024-08-10 18:48:59

fail()

检查是否设置了failbit或badbit。

如果设置了failbit 或badbit,则该函数返回true。当输入操作期间发生除到达文件结尾之外的其他错误时,至少会设置这些标志之一。

ifstream myFile("/LOGS/ex090716.txt");
  if (!myFile.fail()){cout << "Error opening file";}  
  else  {   
    cout << "File opened... \n";
   }
myFile.close(); 

或者

ifstream myFile("/LOGS/ex090716.txt");
  if (!myFile){cout << "Error opening file";}  
  else  {   
    cout << "File opened... \n";
   }
myFile.close();

fail()

Check if either failbit or badbit is set.

The function returns true if either the failbit or the badbit is set. At least one of these flags is set when some error other than reaching the End-Of-File occurs during an input operation.

ifstream myFile("/LOGS/ex090716.txt");
  if (!myFile.fail()){cout << "Error opening file";}  
  else  {   
    cout << "File opened... \n";
   }
myFile.close(); 

OR

ifstream myFile("/LOGS/ex090716.txt");
  if (!myFile){cout << "Error opening file";}  
  else  {   
    cout << "File opened... \n";
   }
myFile.close();
浪荡不羁 2024-08-10 18:48:59

相对路径:不要以 / 开头

相对于程序目录而不是 cd:如果通过 PATH 找到程序,则不能仅使用 argv[0]。我不确定你能做什么是便携式的。您可能想要重复解析符号链接。

在 Linux 上,文件 /proc/self/exe 上的 readlink() 可以工作。

在 Windows 上,这应该有效:

TCHAR path[2048] = {0};
GetModuleFileName( NULL, path, 2048 );
const string exe_path( path );
const string exe_dir( exe_path.substr(0, exe_path.rfind("\\") + 1 );

通常,您应该使用 http://www.boost.org/doc/libs/1_40_0/libs/filesystem/doc/index.htm

Relative path: don't start with /

Relative to program dir rather than cd: you can't just use argv[0] if the program is found via PATH. I'm not sure what you can do that's portable. You may want to resolve symbolic links repeatedly.

On linux, readlink() on the file /proc/self/exe works.

On Windows, this is supposed to work:

TCHAR path[2048] = {0};
GetModuleFileName( NULL, path, 2048 );
const string exe_path( path );
const string exe_dir( exe_path.substr(0, exe_path.rfind("\\") + 1 );

In general, you should use http://www.boost.org/doc/libs/1_40_0/libs/filesystem/doc/index.htm

我ぃ本無心為│何有愛 2024-08-10 18:48:59

perror() 可以相对容易地为您提供问题的详细描述,

int fd = open("/LOGS/ex090716.txt", O_RDONLY);
if(fd == -1) {
    perror("cannot open file");
    exit(1); 
}

但这不是 C++ 风格。

perror() can relative easy give you a detailed description of the problem

int fd = open("/LOGS/ex090716.txt", O_RDONLY);
if(fd == -1) {
    perror("cannot open file");
    exit(1); 
}

however this is not c++'ish.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文