本文介绍了为什么streamstream在0x0A之前插入0x0D字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中使用 ofstream fout(filename);
输出一个无符号字符数组,但是它之间产生一个伪字符。这是导致问题的代码的一部分:

  for(int i = 0; i  fout<< DChuffTable [i]; 

这是数组的定义:

  unsigned char DChuffTable [12] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B} 

在输出文件中,我得到一个假的 0x0D 0x09 0x0A 之间。我在调试模式检查数组,在它要打印,它没有改变。请告诉我您对这个问题的看法。

解决方案

您的讯息串会以文字模式开启,而且 0x0A 是换行符(LF)字符,正在由您的流转换为 0x0D 0x0A

 

以二进制模式打开您的流

code> std :: ofstream fout(filename,std :: ios_base :: out | std :: ios_base :: binary);

不应执行换行结束。



这通常被认为是一个好主意,因为流可以变得奇怪wrt在文本模式下时会闪烁。


I'm outputing an array of unsigned characters in C++ using ofstream fout("filename");but it produces a spurious character in between. This is the part of the code that makes the problem:

for(int i = 0; i < 12; i++)
fout << DChuffTable[i];

and this is the definition of the array:

unsigned char DChuffTable[12] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B};

In the output file I get a spurious 0x0D between 0x09 and 0x0A. I checked the array in debugging mode right before it's going to get printed and it's not changed. Please tell me what you think of this problem.

解决方案

Your stream is opening in text mode, and since 0x0A is the line feed (LF) character, that's being converted by your stream to 0x0D 0x0A, i.e. CR/LF.

Open your stream in binary mode:

std::ofstream fout("filename", std::ios_base::out | std::ios_base::binary);

Then line ending conversions should not be performed.

This is usually considered a good idea anyway, as streams can go bizarre w.r.t. flushing when in text mode.

这篇关于为什么streamstream在0x0A之前插入0x0D字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-23 17:52