簡體   English   中英

無法在 C++ Windows 上打開 .txt 文件

[英]Failing to open .txt file on C++ Windows

我無法使用std::ifstream filefile.open()打開.txt文件。 我想使用間接路徑(從.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";}
}

作為輸出,我得到了失敗按摩。

"C::\\\\inputs\\\\test.txt"不是我必須使用的相對路徑"inputs\\\\test.txt"

感謝評論中的約翰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM