2012-02-01 29 views
0

我正在使用Windows VC++ 2008項目,並嘗試使用fileIO將東西放在子目錄中的日誌文件中。我做了以下內容:C++ fileIO創建/打開文件不工作

void MessageQueue::LogOut(thingEnum _thing){ 
    std::ofstream Output; 
    Output.open("Output/MainLog.txt", std::ios::app); 

    if (Output.is_open()){ 
     // writing stuff 
    } 
    Output.close(); 
} 

我知道的ios ::應用程序會生成一個文件,但是是不是也能夠生成文件夾爲好,或者我需要一個不同的命令生成一個文件夾當我擺脫代碼中的子目錄時,它的工作正常,如果我創建文件夾,我可以將子目錄代碼放回去。

注:據我所知,我應該在技術上打開文件緩衝區,創建流對象的同一行。我沒有這麼做,因爲我打算將.open放入一個case switch(_thing)以訪問多個文件,並且只需更改流。

回答

1

您可以在調用open之前使用_mkdir函數。

http://msdn.microsoft.com/en-us/library/2fkk4dzw(v=vs.90).aspx

#include <direct.h> 

void MessageQueue::LogOut(thingEnum _thing){ 

    if (_mkdir("Output\\") == 0) { 
     std::ofstream Output; 

     Output.open("Output\\MainLog.txt", std::ios::app); 

     if (Output.is_open()){ 
      // writing stuff 
     } 
     Output.close(); 
    } 
    else { 
     // could not create directory 
    } 
} 
0

在Windows上使用CreateDirectory調用來創建文件夾。