2014-03-01 6 views
2

나는 파일에서 인쇄해야하는 구조의 파일 이름 목록을 저장하는 프로그램을 작성했습니다. 파일 이름의 유형은 LPCWSTR에 있으며 파일 이름의 주소 만 문제가됩니다. 또한 스트림 클래스를 사용하는 경우 인쇄했습니다. 또한 wofstream을 시도했지만 "읽기 위치에서 액세스 위반"이 발생합니다.이 문제를 완화하기 위해 사이트를 검색했지만 적절한 솔루션을 얻을 수 없습니다. 많은 사람들이 wctombs 기능을 사용하려고했지만 LPCWSTR을 파일에 인쇄하는 것이 도움이되는지 이해할 수 없습니다.이 문제를 완화하도록 도와주십시오.파일에 LPCWSTR 문자열 인쇄

내 코드 (나는 wcstombs 작동 가져올 수 없습니다) 당신이 다음 변환 할 경우

ofstream out; 
out.open("List.txt",ios::out | ios::app); 
    for(int i=0;i<disks[data]->filesSize;i++) 
        { 
         //printf("\n%ws",disks[data]->lFiles[i].FileName); 
         //wstring ws = disks[data]->fFiles[i].FileName; 
         out <<disks[data]->fFiles[i].FileName << "\n"; 
        } 
        out.close(); 
+0

['표준 : owfstream'] (http://en.cppreference.com/w/cpp/io/bas ic_ofstream), 표준 출력 파일 스트림의'wchar_t' 버전'basic_ofstream '으로 구현하면 원하는 것을 얻을 수 있을까요? 기술적으로, "옳은"것은 변환하는 것이지만 그것은 당신에게 충분할 것입니다. – WhozCraig

+0

@ WhozCraig .. 변환이나 변환 등의 방법을 알려주십시오. – WarriorPrince

+0

가능한 [C++ LPCWSTR을 파일로 인쇄] (http://stackoverflow.com/questions/716653/printing-a-c-lpcwstr-to-a-file) –

답변

3

이 작동합니다, 이렇게 있습니다 : 원시 문자열을 사용

#include <fstream> 
#include <string> 
#include <windows.h> 

int main() 
{ 
    std::fstream File("File.txt", std::ios::out); 

    if (File.is_open()) 
    { 
     std::wstring str = L"русский консоли"; 

     std::string result = std::string(); 
     result.resize(WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, 0, 0)); 
     char* ptr = &result[0]; 
     WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, ptr, result.size(), 0, 0); 
     File << result; 
    } 
} 

(A 때문에 댓글이 std::wstring의 내 사용)에 대해 불평 : 나는 생각하지 않는다

#include <fstream> 
#include <windows.h> 

int main() 
{ 
    std::fstream File("File.txt", std::ios::out); 

    if (File.is_open()) 
    { 
     LPCWSTR wstr = L"русский консоли"; 
     LPCSTR result = NULL; 

     int len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, 0, 0); 

     if (len > 0) 
     { 
      result = new char[len + 1]; 
      if (result) 
      { 
       int resLen = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &result[0], len, 0, 0); 

       if (resLen == len) 
       { 
        File.write(result, len); 
       } 

       delete[] result; 
      } 
     } 
    } 
}