简体   繁体   English

读取二进制文件的问题(ascii和二进制的混合)

[英]problem in reading binary file (mix of ascii and binary)

My code for reading binary file is: 我读取二进制文件的代码是:

dataFile.open(fileName.c_str());
ifstream binData("Trafficlog_Data.txt", ios::in | ios::binary);  //binary data file 
if(!binData) { 
  cout << "Cannot open file.\n"; 
  return -1; 
} 
char *memblock;int nBytes =12;
memblock = new char [nBytes+1];
binData.read(memblock,nBytes);
memblock[nBytes+1]='\0';
std::string message;message.assign(memblock,nBytes);
printf("%s\n",message.c_str());

Now i have given a file as input which contain binary and ascii data 现在我已经给出了一个包含二进制和ascii数据的输入文件
RFB 003.003 RFB 003.003
RFB 003.003 RFB 003.003
--some binary data-- - 一些二进制数据 -

When I am reading first 12 bytes of file which is "RFB 003.003\\n" but it prints "RFB 003.003=". 当我读取前12个字节的文件“RFB 003.003 \\ n”但它打印“RFB 003.003 =”。 Can anyone tell where im getting it wrong please. 任何人都可以告诉我哪里错了。 promblem is not with '\\0'. promblem不是'\\ 0'。 problem is it is not reading "RFB 003.003\\n" . 问题是它没有读“RFB 003.003 \\ n”。 IS it because this file is mix of binary and ascii data 是因为这个文件是二进制和ascii数据的混合

你没有为memblock分配内存:

char *memblock = new char[nBytes+1];

Change: 更改:

memblock[nBytes+1]='\0';

to: 至:

memblock[nBytes]='\0';

Let's say you read in six bytes to memblock , that goes into positions 0 through 5 inclusive: 假设你用6个字节读到了memblock ,它进入了05位置:

  0   1   2   3   4   5    6    7
+---+---+---+---+---+----+---+---+
| H | E | L | L | O | \n | ? | ? |
+---+---+---+---+---+----+---+---+

(the ? areas still contain whatever rubbish was there before). ?区域仍然包含之前的垃圾)。

You then need to put the null terminator at position 6 rather than position 7 as your code is doing. 然后,您需要将null终止符放在位置6而不是位置7,就像您的代码所做的那样。

By placing the null terminator too far to the "right", you're including that first ? 通过将空终止符放置到“右”的位置太远,您首先要包含它? position, which could be holding anything. 位置,可以持有任何东西。


That's what's causing your specific problem. 这就是造成你特定问题的原因。 You also have an issue that you're not allocating space to hold the data you're reading in. You just have a char * but you're not actually initialising it to point to usable memory. 您还遇到一个问题,即您没有分配空间来容纳您正在读取的数据。您只需要一个char *但实际上并没有初始化它以指向可用内存。 That's almost certainly going to cause problems. 这几乎肯定会导致问题。

Probably the simplest solution is to define it as: 可能最简单的解决方案是将其定义为:

char memblock[nBytes+1];

Although I see you've fixed that in your question now so it's of little consequence. 虽然我现在看到你已经在你的问题中解决了这个问题,但这并不重要。 The actual problem (putting the null byte at the wrong location) is covered above. 上面介绍了实际问题(将空字节放在错误的位置)。

You're off-by-one: just do memblock[nBytes]='\\0'; 你是一个人:只做memblock[nBytes]='\\0'; The index starts at 0, so if nBytes is 0 you're writing at the first position, if nBytes is 1 you're writing at the second position and so on. 索引从0开始,所以如果nBytes为0,则在第一个位置写入,如果nBytes为1,则在第二个位置写入,依此类推。 By doing nBytes + 1 you actually jumped ahead one position and left one garbage byte at the end of the string. 通过执行nBytes + 1您实际上跳过一个位置并在字符串末尾留下一个垃圾字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM