同样一段代码
FILE *fpf = fopen(filePath, "rb"); fseek(fpf, 98, SEEK_CUR); unsigned short int receive_arr[1024] = { 0 }; fread((char*)&receive_arr, sizeof(receive_arr), 1, fpf); int tmp_count = 0; for (auto value : receive_arr)cout << "count: " << ++tmp_count << " value: " << value << endl;
就是正确的
而使用C++ 的fstream:
fstream fpf(filePath, ios::binary); unsigned short* receive_arr = nullptr; try { receive_arr = new unsigned short(1024); } catch (bad_alloc) { cerr << "bad_alloc in" << __LINE__ << endl; } fpf.seekg(sizeof(char) * 98,ios_base::beg); fpf.read((char*)receive_arr,2048); fpf.close();
读出来的receive_arr的值就是不对的,这是为什么?
哆啦的时光机
慕盖茨4494581
相关分类