-2
我正在嘗試創建一個文件,其中包含必須在特定文件夾中放置的文本。但是,它被創建,但與exe文件位於同一個文件夾中。這裏的代碼:C++ Ofstream不在特定位置創建文件
const char* name;
char path[200];
int main()
{
cin.getline(path, 230,'\n');
DIR *dir;
name = path;
struct dirent *ent;
if ((dir = opendir(name)) != NULL)
{
while ((ent = readdir(dir)) != NULL)
{
ofstream file(path, ios::out | ios::trunc);
file.open("somename");
file << ent->d_name + " something";
file.close();
}
closedir(dir);
}
else
{
perror("");
}
}
我得到什麼錯誤?
'file.open( 「有些名」);'將創建在當前工作目錄中的文件,沒有任何給出的具體呸路徑。 – user0042
請考慮添加resp。包括在你的源的開始。通常在函數本身的手冊頁中提到了庫函數的必要內容。 (它們在[MCVE](http://stackoverflow.com/help/mcve)中丟失,希望不在您的「真實」程序中。) – Scheff
如果要將文件保存到特定目錄中,請提供resp 。相對或絕對文件路徑(包含目錄)或更改當前目錄。你可以在程序中使用['chdir()'](http://man7.org/linux/man-pages/man2/chdir.2.html)。 – Scheff