简体   繁体   English

用fstream读取二进制文件

[英]Reading binary file with fstream

I am reading a binary file with fstream and storing the information in an array of characters: 我正在使用fstream读取二进制文件,并将信息存储在字符数组中:

int dataLength = 32;
int counter = 0;
char data[dataLength];
char PMTone[dataLength/4];
std::fstream *fs = new std::fstream(inputFileName,std::ios::in|std::ios::binary);
fs->read((char *)&data, dataLength);
//of the 32 characters in data[], I need first, 5th etc elements:
//fill first pmt info
for(int i=0; i<(dataLength/4); i++){
PMTone[i]=data[counter];
counter+=4;
}

Now I'm setting PMTone[7] to be a as a test: 现在,我将PMTone [7]设置为a作为测试:

PMTone[7] = "a";

I get the error: 我得到错误:

mdfTree.cpp:92:21: error: invalid conversion from 'const char*' to 'char' [-fpermissive] mdfTree.cpp:92:21:错误:从'const char *'到'char'的无效转换[-fpermissive]

I don't understand why the elements in PMTone[] are pointers to chars, when PMTone[] was defined as an array of chars. 当PMTone []被定义为字符数组时,我不明白为什么PMTone []中的元素是指向chars的指针。

When I do treat PMTone[] as an array of pointers to chars: 当我将PMTone []视为指向chars的指针数组时:

(*PMTone)[7] = "a";

I get another error that I don't understand: 我收到另一个我不理解的错误:

mdfTree.cpp:91:18: error: invalid types 'char[int]' for array subscript mdfTree.cpp:91:18:错误:数组下标的类型'char [int]'无效

This seems to imply that the compiler doesn't consider PMTone[] to be an array at all, but simply a pointer to char. 这似乎暗示编译器根本不认为PMTone []是数组,而只是指向char的指针。

Can anyone shed light on what is happening here? 任何人都可以了解这里发生的事情吗? Why has PMTone[] become an array of pointers to chars? 为什么PMTone []成为指向chars的指针数组?

The literal "a" is not a character. 文字"a"不是字符。

You need: 你需要:

PMTone[7] = 'a';

"a" is not a character it is an array of characters, 'a' followed by a null terminator. "a"不是字符,而是字符数组,“ a”后跟一个空终止符。

You would need 你需要

 PMTone[7] = 'a';

with single quote. 单引号。 Incidentally I am surprised that it compiles earlier because dataLength wasn't declared as const. 顺便说一句,我很惊讶它早先编译,因为dataLength没有声明为const。

PMTone itself is of type char[8] which is an array of characters. PMTone本身的类型为char[8] ,它是一个字符数组。 However it decays to a pointer and (*PMTone) is the first element, of type char 但是,它会衰减为一个指针,并且(*PMTone)是第一个元素,类型为char

An array is a way of storing values by having the address of the first element and accessing other elements by index. 数组是一种通过拥有第一个元素的地址并通过索引访问其他元素来存储值的方式。 An array is basically a pointer to the first element. 数组基本上是指向第一个元素的指针。

When you create an array of chars 当您创建chars数组时

char arr[n];

program actually creates room for n characters in the memory. 程序实际上在内存中为n个字符留出了空间。

When accessing arr[0] let's say like this: 当访问arr[0]我们这样说:

arr[2] = 'a';

program is actually doing this: 程序实际上是在这样做:

*(arr + 2) = 'a';

meaning you are accessing a character which is stored 2 Bytes further from the memory location of the first element in the array (hence, the arr + 2 ), with the first element being arr[0] . 表示您正在访问的字符距数组中第一个元素的存储位置(因此, arr + 2 )的存储位置相距2个字节,并且第一个元素为arr[0] The address of the first element is &(arr[0]) or, arr . 第一个元素的地址为&(arr[0])arr Both are the same values. 两者是相同的值。

The bug is where you tried saving a string "a" into a char . 该错误是您尝试将字符串"a"char

The problem is you tried fixing it by dereferencing a dereferenced pointer: 问题是您尝试通过取消引用已取消引用的指针来修复它:

(*PMTone)[7] = "a"; // is the same as
(PMTone[0])[7] = "a"; // when it should be 
PMTone[7] = 'a';

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

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