有必要将有关机器的信息写入文件,并能够补充该文件并读取一定数量的记录。从二进制文件读取时,输出为:
Automobile 1�犎�
Brand: dasf0�t�UMileage: asdf芎�
Price: asdf 芎�
Automobile 2�犎�Brand: afdsfdas0�t�U`�t�UMileage: dafs芎�
Price: fdas
(对于字段值中无意义的一组字母,我深表歉意)。据我了解,新行的转义序列在某种程度上被错误地解释了。对于录制,我使用以下功能:
void binary_write(std::ofstream& ofs, std::string& s){
ofs.write((char*)&s, sizeof(s));
}
这是我写的几行:
first_line = "Automobile " + std::to_string(count+1);
brand = "Brand: " + brand;
manufacture = "Manufacture: " + manufacture;
release = "Year of release: " + release;
mileage = "Mileage: " + mileage;
price = "Price: " + price;
阅读代码:
while(ifs.read(&temp, sizeof(char)))
std::cout << temp;
谁能建议如何使用二进制文件正确组织 I/O?提前致谢。