2012-02-16 6 views
0

파일을 데스크톱에 저장하는 방법을 찾고 싶습니다. 모든 사용자마다 다른 사용자 이름이 있으므로 다음 코드는 다른 사람의 데스크톱 경로를 찾는 데 도움이됩니다. 하지만 다음을 데스크톱에 저장하려면 어떻게해야합니까? file.open(appData +"/.txt");이 작동하지 않습니다. 예를 보여 주시겠습니까? 마이크로 소프트 비주얼 스튜디오 2010C++에서 바탕 화면에 파일을 저장하는 방법?

#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; 
} 

, 윈도우 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

+3

두 개의 문자 포인터를 추가한다고 생각합니다. 문자열을 연결하려고합니다. 스트링 스트림을 사용하고 두 가지 모두를 사용하도록 제안 하시겠습니까? 또한 실제로는 .txt 확장명 만 파일 이름에 넣는 것처럼 보이지 않습니다. 최근에 Windows를 많이 사용하지는 않았지만이 문제에 대해 조금 짚어 보지 않겠습니까? – BoBTFish

+0

@Tudor C++과 C는 Windows에서도 디렉토리 구분 기호로'/'를 사용할 수 있습니다! 당신은 파일 이름에 대해 옳았습니다.'.txt'는 형편없는 이름입니다. –

답변

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.open (file_path.str(). c_str());은 함수 호출에서 너무 적은 인수를 말합니다 : –

+0

@NewGuy, #include '그리고 변수'std :: ostringstream file_path; – hmjd

+0

예, 했어요.내가 원래의 질문을 당신이 설명한 방식대로 업데이트했습니다. –

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

이 파일 경로에는 파일 이름이 없습니다.

이 함수 호출도 유효하지 않습니다. 두 번째 매개 변수는 열린 형식으로 전달해야합니다.

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

은 올바른 것입니다.

+1

그러나 TCHAR 배열의 +는 문자열을 연결하지 않으며 포인터 만 추가하면 야생 포인터가됩니다. – nos

+0

예제를 보여 주셔서 감사합니다. 하지만'file.open (appData + "/ file.txt", fstream :: out);을 사용할 때''/file.txt ''를 그렇게 사용할 수 없습니다. 오류 : 표현식은 integarl 또는 enum 유형이어야합니다. –

+0

'ios :: out'이'std :: ostream'의 기본값이라고 생각합니다. 명시 적으로 전달할 필요가 없습니다. – sbi

0

누락 및/또는 여분의 백 슬래시 (\) 세트를 처리하는 경로를 연결하려면 PathAppend을 사용해야합니다.

0

나는이 사용할 수 있는지 여부를 확실하지 않다 :

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

당신은 시도 할 수 있습니다.

관련 문제