[英]ofstream: cannot open the file
我面臨一個問題,程序無法打開我想寫的文件。 最小,完整和可變化的代碼如下所示:
#include <cstdio>
#include <fstream>
using namespace std;
int main(){
string root_path = "E:\\160818\\";
string file_path = root_path + "haar_data.txt";
ofstream haar_file(file_path.c_str());
if(!haar_file) // < -------- File cannot be open
{
cout<<"Error opening file for writing\n";
return 1;
}
haar_file.close();
return 0;
}
我的編譯器是VS2008。 屏幕中的輸出是
打開文件進行寫入時出錯。
錯誤是什么? 我想打開這個文件寫點東西。
我已編譯您的代碼並使用它,它工作。
請注意,您沒有在提供的代碼中包含iostream,因此我必須添加它,因為'cout'表達式。 請注意,從c ++ 11開始,您不需要使用.C_str()來打開文件。 請注意,如果您的文件不存在,因為您使用ofstream(使用ios :: out打開),無論如何都會創建您的文件。 您使用管理員權限嗎? 你的路徑是否正確(文件夾名稱錯誤?)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.