2012-02-16 348 views
0

我想找到一種方法將文件保存到桌面。由於每個用戶都有不同的用戶名,我發現下面的代碼會幫助我找到別人桌面的路徑。但是,我怎樣才能將以下內容保存到桌面? file.open(appData +"/.txt");不起作用。你能告訴我一個例子嗎?如何將文件保存到C++的桌面?

#include <iostream> 
#include <windows.h> 
#include <fstream> 
#include <direct.h> 
#include <shlobj.h> 
using namespace std; 
int main() 
{ 
    ofstream file; 

    TCHAR appData[MAX_PATH]; 
    if (SUCCEEDED(SHGetFolderPath(NULL, 
            CSIDL_DESKTOPDIRECTORY | CSIDL_FLAG_CREATE, 
            NULL, 
            SHGFP_TYPE_CURRENT, 
            appData))) 

    wcout << appData << endl; //This will printout the desktop path correctly, but 
    file.open(appData +"file.txt"); //this doesn't work 
    file<<"hello\n"; 
    file.close(); 
    return 0; 
} 

微軟的Visual Studio 2010,Windows 7中,C++控制檯

更新:


#include <iostream> 
#include <windows.h> 
#include <fstream> 
#include <direct.h> 
#include <shlobj.h> 
#include <sstream> 
using namespace std; 
int main() 
{ 
    ofstream file; 

    TCHAR appData[MAX_PATH]; 
    if (SUCCEEDED(SHGetFolderPath(NULL, 
            CSIDL_DESKTOPDIRECTORY | CSIDL_FLAG_CREATE, 
            NULL, 
            SHGFP_TYPE_CURRENT, 
            appData))) 

    wcout << appData << endl; //This will printout the desktop path correctly, but 
    std::ostringstream file_path; 
    file_path << appData << "\\filename.txt";//Error: identifier file_path is undefined 

    file.open(file_path.str().c_str()); //Error:too few arguments in function call 
    return 0; 
} 
+0

Windows路徑與'\\'否?而且您還需要該文件的名稱。 – Tudor 2012-02-16 11:09:50

+3

我想你正在添加兩個字符指針。你想連接字符串。建議使用一個stringstream,並將其推入到它?另外,你實際上似乎沒有在文件名中加上.txt擴展名。最近沒有使用過Windows,但是不會在這方面做些什麼? – BoBTFish 2012-02-16 11:11:36

+0

@Tudor即使在Windows上,C++和C也可以使用'/'作爲目錄分隔符!儘管你對文件名稱是正確的,'.txt'是一個糟糕的名字。 – 2012-02-16 11:14:23

回答

3

可以使用appData +"/.txt"無法串連TCHAR陣列。使用stringstream從中構建的路徑和提取文件的完整路徑:

#include <sstream> 

... 

std::ostringstream file_path; 
file_path << appData << "\\filename.txt"; 

file.open(file_path.str().c_str()); 

編輯:

以下編譯並執行正確的,對我來說與VS2010:

#include <iostream> 
#include <windows.h> 
#include <fstream> 
#include <direct.h> 
#include <shlobj.h> 
#include <sstream> 
#include <tchar.h> 
using namespace std; 
int main() 
{ 
    ofstream file; 

    TCHAR appData[MAX_PATH]; 
    if (SUCCEEDED(SHGetFolderPath(NULL, 
            CSIDL_DESKTOPDIRECTORY | CSIDL_FLAG_CREATE, 
            NULL, 
            SHGFP_TYPE_CURRENT, 
            appData))) 

    wcout << appData << endl; 
    std::basic_ostringstream<TCHAR> file_path; 
    file_path << appData << _TEXT("\\filename.txt"); 

    file.open(file_path.str().c_str()); 
    file<<"hello\n"; 
    file.close(); 

    return 0; 
} 
+0

它表示file_path在'file_path << appData <<「\\ filename.txt」;「中是未定義的?不知道爲什麼。 (file_path.str()c_str())和'File.open方法;'說,在函數調用 – 2012-02-16 11:26:46

+0

@NewGuy參數太少,沒有你的#include'',並添加變量'的std :: ostringstream FILE_PATH;'? – hmjd 2012-02-16 11:29:42

+0

是的,我做過。我已經更新了我原來的問題,就像你解釋 – 2012-02-16 11:40:14

0
file.open(appData +"/.txt"); 

在這個文件路徑中沒有文件名。

此函數調用無效。您應該將第二個參數作爲開放類型傳遞。

file.open(appData +"/file.txt", fstream::out); 

是正確的。

+1

但是,TCHAR數組上的+不會連接字符串,它只會添加指針,並最終生成一個通配指針 – nos 2012-02-16 11:15:19

+0

感謝您向我展示一個示例。但是當我使用'file.open(appData +「/ file.txt」,fstream :: out);'it says,'「/file.txt」'不能以這種方式使用。錯誤:表達式必須有整數或枚舉類型 – 2012-02-16 11:17:01

+0

我認爲'ios :: out'是'std :: ostream'的默認值。沒有必要明確地傳遞它。 – sbi 2012-02-16 11:19:14

0

您應該使用PathAppend連接路徑,這些路徑將處理丟失和/或額外的反斜槓(\)字符集。

0

我不知道這是否是可用的:

file.open("%userprofile%\\Desktop\\file.txt", fstream::out);

你可以試試。