【问题标题】:C++ char buffer pointer errorC++ char缓冲区指针错误
【发布时间】:2015-08-03 12:21:54
【问题描述】:

我正在使用一个函数来读取假脱机文件并使用输出设置缓冲区。 该函数返回 OK 状态并正确设置readBytes。它还通知读取操作已到达文件末尾。

char* splFileContent = new char[3000];
ULONG readBytes;
int z = cwbOBJ_ReadSplF(splFile, splFileContent, 500, &readBytes, 0);
//z value is REACHED END OF FILE or OK if read but didn't reach the end of the file.

在尝试将 char 缓冲区转换为字符串时出现问题,我得到 "4Ä" 作为字符串值...

我通过这种方式将 char 缓冲区转换为字符串:

stringstream s;
s << splFileContent;
string bufferContent = s.str();

我做错了什么?

【问题讨论】:

  • 您可以使用string bufferContent(splFileContent, readBytes) ;转换为字符串

标签: c++ pointers char buffer


【解决方案1】:

看起来splFileContent 是二进制内容而不是可打印字符。

文件的开头可能包含某种 BOM,例如Unicode 指标。如果是,您应该先读入 BOM,然后再读入文件的其余部分。

注意:除非此处的文件读取函数添加了NULL,否则请务必添加一个。

【讨论】:

  • 如何将二进制字符转换为 ascii?谢谢。
  • 通常你将字节宽的二进制转换为两个字节宽的十六进制字符串(每个字符都在 [0-9A-F] 范围内)。您可以使用流、sprintf 函数等。有几种流行的技术。
猜你喜欢
  • 1970-01-01
  • 2011-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多