无法在C ++ Windows上打开.txt文件

时间:2020-09-17 14:26:33

标签: c++ windows file debugging memory

我无法使用.txtstd::ifstream file打开file.open()文件。我想使用间接路径(从.exe文件所在的文件夹-\program_folder中开始),但是使用完整的路径(C:\Users\Rafael\Desktop\C++\program_folder\inputs\test.txt)也无效。

#include <iostram>
#include <vector>
#include <string>
#include <fstream>

int main(){
   char c;
   std::vector<std::string> inputs;
   inputs.push_back("C:\\inputs\\test.txt");
   int a; int b;
   std::ifstream file;
   file.open(inputs[0]);
   if (file.is_open()){
      c = file.get();
      file.close();
   }
   else {std::cout << "\nfail to open file";}
}

作为输出,我得到了失败的消息。

1 个答案:

答案 0 :(得分:0)

"C::\\inputs\\test.txt"不是我必须使用的"inputs\\test.txt"

相对路径

感谢约翰的评论。

相关问题