質問

I have the following code, which I am trying ot change an integer number to wstring then I can pass it to a text file. but the problm is that when it reached to the _itow_s(vec, img_stamp, 10); line it change the number to a negative number. any body know what is the problem?

wchar_t img_stamp[64];
   while(c<_timeStamp.size()){      
        vec=_timeStamp.at(c);
        _itow_s(vec, img_stamp, 10);    
        _data +=std::wstring(img_stamp);
        _data +=std::wstring(L"\r\n");
        c++;
    }


    HANDLE hFile; 
    DWORD wmWritten;    
    hFile = CreateFile( "D:\\test\\testing.txt" ,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); 
    if(hFile!=NULL){

        DWORD len=sizeof(_data);
        WriteFile(hFile, _data.c_str(), _data.size() * 2, &wmWritten, NULL);
    }   
    CloseHandle(hFile); 
役に立ちましたか?

解決 2

Why don't you use a wstringstream?

wstringstream ws;
for (...) { // some loop
  ws << myint << ", ";
}
//now, you can use ws.str() to extract a wstring from the stream

他のヒント

C++11 added a function called 'std::to_string()' which converts a number, float, or long long to a std::string. There is also a related set of functions for wstrings: std::to_wstring().

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top