GMemoryStream和std::iostream转换

3 篇文章 0 订阅

说明:

1
2
3
4
5
6
typedef  QBuffer GBuffer;
class  GMemoryStream :  public  GBuffer
{
     ...
};
class  std::stringstream :  public  std::iostream{...};

1.GMemoryStream转换为std::iostream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 1.定义 QBuffer
QBuffer buff;
buff.open(QBuffer::ReadWrite);
buff.write( "liujm" , 5);
// 2.定义 stringstream
std::stringstream strBuff;
// 3.将 QBuffer 写入 stringstream
strBuff.write(buff.buffer().data(), buff.size());
 
// 测试
char * arr =  new  char [buff.size() + 1];
strBuff >> arr;
cout << arr << endl;      // liujm
delete  arr;
For example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void  loadFromStream(std::iostream &io)
{
     io.seekg(0, ios::end);   // the ending of a stream
     int  nCount = io.tellg();
     io.seekg(0, ios::beg);   // the beginning of a stream
 
     char * arr =  new  char [nCount + 1];
     io.read(arr, nCount);
     *(arr + nCount) =  '\0' ;
     cout << arr << endl;  
     delete  arr;
}
 
void  bufferToIostream()
{   
     QBuffer buff;
     buff.open(QBuffer::ReadWrite);
     buff.write( "liujm" , 5);
     std::stringstream strBuff;
     strBuff.write(buff.buffer().data(), buff.size());
     loadFromStream(strBuff);
}

2.std::iostream转换为GMemoryStream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 1.定义 stringstream
std::stringstream strBuff;
strBuff.write( "liujm" , 5);
strBuff <<  '\0' ;
strBuff.write( "liujm" , 5);
// 2.定义 QBuffer
QBuffer buff;
buff.open(QIODevice::ReadWrite);
// 3.将 stringstream 中的流写入 QBuffer
buff.write(strBuff.str().c_str(), strBuff.str().size());
 
// 测试
char * arr =  new  char [1 + strBuff.str().size()];
buff.seek(0);
buff.read(arr, strBuff.str().size());
*(arr + strBuff.str().size()) = 0;
cout << arr << endl;     // 注意:此处打印出的结果只有liujm,而不是liujm\0liujm,因为对于c风格的字符串\0就是结尾。QBuffer 流中的数据是 liujm\0liujm
delete  arr;
For example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void  saveToStream(std::iostream &io)
{
     // ...
     io.write( "liujm" , 5);
}
 
void  iostreamToBuffer()
{
     std::stringstream strBuff;
     saveToStream(strBuff);
     QBuffer buff;
     buff.open(QIODevice::ReadWrite);
     buff.write(strBuff.str().c_str(), strBuff.str().size());
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是TCHAR和std::string之间的转换方法: 1.将TCHAR转换std::string: ```c++ #include <string> #include <tchar.h> TCHAR tcharStr[] = _T("TCHAR string"); std::string str = std::string(tcharStr, tcharStr + _tcslen(tcharStr)); ``` 2.将std::string转换为TCHAR: ```c++ #include <string> #include <tchar.h> std::string str = "std::string"; TCHAR tcharStr[100]; _tcscpy_s(tcharStr, CA2T(str.c_str())); ``` 其中,CA2T是ATL/MFC库中的一个宏,用于将const char*转换为TCHAR*。 以下是std::string和LPCWSTR之间的转换方法: 1.将std::string转换为LPCWSTR: ```c++ #include <string> #include <Windows.h> std::string str = "std::string"; LPCWSTR lpcwstr = CA2W(str.c_str()); ``` 其中,CA2W是ATL/MFC库中的一个宏,用于将const char*转换为LPCWSTR。 2.将LPCWSTR转换std::string: ```c++ #include <string> #include <Windows.h> LPCWSTR lpcwstr = L"LPCWSTR string"; std::wstring wstr(lpcwstr); std::string str(wstr.begin(), wstr.end()); ``` 以下是完整的代码示例: ```c++ #include <iostream> #include <string> #include <tchar.h> #include <Windows.h> using namespace std; int main() { // TCHAR to std::string TCHAR tcharStr[] = _T("TCHAR string"); std::string str1 = std::string(tcharStr, tcharStr + _tcslen(tcharStr)); cout << "TCHAR to std::string: " << str1 << endl; // std::string to TCHAR std::string str2 = "std::string"; TCHAR tcharStr2[100]; _tcscpy_s(tcharStr2, CA2T(str2.c_str())); wcout << "std::string to TCHAR: " << tcharStr2 << endl; // std::string to LPCWSTR std::string str3 = "std::string"; LPCWSTR lpcwstr = CA2W(str3.c_str()); wcout << "std::string to LPCWSTR: " << lpcwstr << endl; // LPCWSTR to std::string LPCWSTR lpcwstr2 = L"LPCWSTR string"; std::wstring wstr(lpcwstr2); std::string str4(wstr.begin(), wstr.end()); cout << "LPCWSTR to std::string: " << str4 << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值