Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

c++ - Reading binary file with fstream

I am reading a binary file with fstream and storing the information in an array of characters:

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";

I get the error:

mdfTree.cpp:92:21: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]

I don't understand why the elements in PMTone[] are pointers to chars, when PMTone[] was defined as an array of chars.

When I do treat PMTone[] as an array of pointers to 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

This seems to imply that the compiler doesn't consider PMTone[] to be an array at all, but simply a pointer to char.

Can anyone shed light on what is happening here? Why has PMTone[] become an array of pointers to chars?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The literal "a" is not a character.

You need:

PMTone[7] = 'a';

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...