파일을 데스크톱에 저장하는 방법을 찾고 싶습니다. 모든 사용자마다 다른 사용자 이름이 있으므로 다음 코드는 다른 사람의 데스크톱 경로를 찾는 데 도움이됩니다. 하지만 다음을 데스크톱에 저장하려면 어떻게해야합니까? 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;
}
Windows 경로에 '\\'아니오가 있습니까? 그리고 파일 이름도 필요합니다. – Tudor
두 개의 문자 포인터를 추가한다고 생각합니다. 문자열을 연결하려고합니다. 스트링 스트림을 사용하고 두 가지 모두를 사용하도록 제안 하시겠습니까? 또한 실제로는 .txt 확장명 만 파일 이름에 넣는 것처럼 보이지 않습니다. 최근에 Windows를 많이 사용하지는 않았지만이 문제에 대해 조금 짚어 보지 않겠습니까? – BoBTFish
@Tudor C++과 C는 Windows에서도 디렉토리 구분 기호로'/'를 사용할 수 있습니다! 당신은 파일 이름에 대해 옳았습니다.'.txt'는 형편없는 이름입니다. –