关于C++中的wstringstream和stringstream?

2024-11-18 14:23:24
推荐回答(5个)
回答(1):

修改并运行通过,你自己看吧,有不明白的再问
#include
#include
#include
#include

using namespace std;
LPTSTR SlotName = TEXT("sample_mailslot.txt");
BOOL WriteSlot(HANDLE hSlot, LPCTSTR lpszMessage)
{
BOOL fResult;
DWORD cbWritten;
fResult = WriteFile(hSlot, lpszMessage, (DWORD)(lstrlen(lpszMessage)+1)*sizeof(TCHAR), &cbWritten, (LPOVERLAPPED) NULL);
if (!fResult)
{
printf("WriteFile failed with %d.\n", GetLastError());
return FALSE;
}
printf("Slot written to successfully.\n");
return TRUE;
}
int main()
{
HANDLE hFile;
string strtmp;
wstring wstrtmp;
hFile = CreateFile(SlotName, GENERIC_WRITE, FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("CreateFile failed with %d.\n", GetLastError());
return FALSE;
}
bool bFlag = true;
while(bFlag)
{
cout << "input your order please:" << endl;
cin >> strtmp;
if (strtmp == "exit")
{
bFlag = false;
}
else
{
std::wstringstream wstream;
std::stringstream stream;

stream << strtmp;
wstream << stream;
wstream >> wstrtmp;
std::cout < WriteSlot(hFile, wstrtmp.c_str() );
}
}
CloseHandle(hFile);
}

回答(2):

修改并运行通过,你自己看吧,有不明白的再问
#include
#include
#include
#include

using namespace std;
LPTSTR SlotName = TEXT("sample_mailslot.txt");
BOOL WriteSlot(HANDLE hSlot, LPCTSTR lpszMessage)
{
BOOL fResult;
DWORD cbWritten;
fResult = WriteFile(hSlot, lpszMessage, (DWORD)(lstrlen(lpszMessage)+1)*sizeof(TCHAR), &cbWritten, (LPOVERLAPPED) NULL);
if (!fResult)
{
printf("WriteFile failed with %d.\n", GetLastError());
return FALSE;
}
printf("Slot written to successfully.\n");
return TRUE;
}
int main()
{
HANDLE hFile;
string strtmp;
wstring wstrtmp;
hFile = CreateFile(SlotName, GENERIC_WRITE, FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("CreateFile failed with %d.\n", GetLastError());
return FALSE;
}
bool bFlag = true;
while(bFlag)
{
cout << "input your order please:" << endl;
cin >> strtmp;
if (strtmp == "exit")
{
bFlag = false;
}
else
{
std::wstringstream wstream;
std::stringstream stream;

stream << strtmp;
wstream << stream;
wstream >> wstrtmp;
std::cout < WriteSlot(hFile, wstrtmp.c_str() );
}
}
CloseHandle(hFile);

回答(3):

wstringstream是创建类型在 wchar_t 模板参数专用的 basic_stringstream
stringstream是字符串流。它将流与存储在内存中的string对象绑定起来。在多种数据类型之间实现自动格式化

回答(4):

#include
#include
#include
#include

using namespace std;

LPTSTR SlotName = TEXT("\\\\.\\mailslot\\sample_mailslot");

BOOL WriteSlot(HANDLE hSlot, LPTSTR lpszMessage)
{
BOOL fResult;
DWORD cbWritten;

fResult = WriteFile(hSlot,
lpszMessage,
(DWORD) (lstrlen(lpszMessage)+1)*sizeof(TCHAR),
&cbWritten,
(LPOVERLAPPED) NULL);

if (!fResult)
{
printf("WriteFile failed with %d.\n", GetLastError());
return FALSE;
}

printf("Slot written to successfully.\n");

return TRUE;
}

int main()
{
HANDLE hFile;
string strtmp;
wstring wstrtmp;

hFile = CreateFile(SlotName,
GENERIC_WRITE,
FILE_SHARE_READ,
(LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);

if (hFile == INVALID_HANDLE_VALUE)
{
printf("CreateFile failed with %d.\n", GetLastError());
return FALSE;
}

bool bFlag = true;
while(bFlag);
{
cout << "input your order please:" << endl;
cin >> strtmp;

if (strtmp == "exit")
{
bFlag = false;
}
else
{
std::wstringstream wstream;
std::stringstream stream;
stream << wstrtmp; // error C2679出现的地方
stream >> strtmp;
std::cout <WriteSlot(hFile, wstrtmp.c_str() ); // error C2664出现的地方

}
}
CloseHandle(hFile);
}

回答(5):

压标亘古未有下大雨夺下大雨夺下大雨