C++ 如何用c+读取和写入8位整数单位形式的数据+;文件函数
是否可以将数据存储为从0到255的整数形式,而不是8位字符。虽然两者都是相同的东西,但我们如何才能做到这一点,例如,使用write()函数? 是否可以将任何整数直接转换为char,反之亦然?是这样的吗C++ 如何用c+读取和写入8位整数单位形式的数据+;文件函数,c++,casting,fstream,portability,C++,Casting,Fstream,Portability,是否可以将数据存储为从0到255的整数形式,而不是8位字符。虽然两者都是相同的东西,但我们如何才能做到这一点,例如,使用write()函数? 是否可以将任何整数直接转换为char,反之亦然?是这样的吗 { int a[1]=213; write((char*)a,1); } 及 { int a[1]; 读((字符*)a,1); cout它将起作用,但有一些警告: 使用reinterpret_cast(x)而不是(char*)x来明确说明您正在执行通常不安全的强制转换 siz
{
int a[1]=213;
write((char*)a,1);
}
及
{
int a[1];
读((字符*)a,1);
cout它将起作用,但有一些警告:
使用reinterpret_cast(x)
而不是(char*)x
来明确说明您正在执行通常不安全的强制转换
sizeof(int)
因平台而异,因此您可能希望使用
中的固定大小整数类型,例如int32\t
平台之间也可能有所不同,因此在写入文件时应注意平台字节顺序,并将字节顺序交换为一致的格式。您可以手动交换和交换字节,或在主机和网络(big-endian)字节顺序之间进行转换
此外,作为一个实际问题,我建议您更喜欢基于文本的格式,它们不太紧凑,但在出错时更容易调试,因为您可以在任何文本编辑器中检查它们。如果确定加载和解析这些文件太慢,则考虑移动到二进制格式。
您所显示的代码将写入第一个文件。a[0]
对象表示形式的(最低地址)字节-可能是也可能不是值为213
的字节。int
的特定对象表示形式由IME实现定义
写入值为213
的一个字节的可移植方式是
unsigned char c = a[0];
write(&c, 1);
你的想法是对的,但可能需要一些改进
{
int intToWrite = 213;
unsigned char byteToWrite = 0;
if ( intToWrite > 255 || intToWrite < 0 )
{
doError();
return();
}
// since your range is 0-255, you really want the low order byte of the int.
// Just reading the 1st byte may or may not work for your architecture. I
// prefer to let the compiler handle the conversion via casting.
byteToWrite = (unsigned char) intToWrite;
write( &byteToWrite, sizeof(byteToWrite) );
// you can hard code the size, but I try to be in the habit of using sizeof
// since it is better when dealing with multibyte types
}
{
int a = 0;
unsigned char toRead = 0;
// just like the write, the byte ordering of the int will depend on your
// architecture. You could write code to explicitly handle this, but it's
// easier to let the compiler figure it out via implicit conversions
read( &toRead, sizeof(toRead) );
a = toRead;
cout<<a;
}
{
int intToWrite=213;
无符号字符byteToWrite=0;
如果(intToWrite>255 | | intToWrite<0)
{
多罗();
return();
}
//因为您的范围是0-255,所以您确实需要int的低位字节。
//仅仅读取第一个字节可能对您的体系结构有效,也可能无效
//更愿意让编译器通过强制转换来处理转换。
byteToWrite=(无符号字符)intToWrite;
写入(&byteToWrite,sizeof(byteToWrite));
//您可以硬编码大小,但我尽量养成使用sizeof的习惯
//因为它在处理多字节类型时更好
}
{
int a=0;
无符号字符读取=0;
//就像写入一样,int的字节顺序将取决于
//您可以编写代码来显式地处理这个问题,但是
//更容易让编译器通过隐式转换来理解它
阅读(&toRead,sizeof(toRead));
a=探路者;
cout“integer to char”-yes,“反之亦然”-no(由于地址对齐)。此外,在第一种情况下(合法的情况),您将在big-endian和little-endian之间存在兼容性问题。
{
int intToWrite = 213;
unsigned char byteToWrite = 0;
if ( intToWrite > 255 || intToWrite < 0 )
{
doError();
return();
}
// since your range is 0-255, you really want the low order byte of the int.
// Just reading the 1st byte may or may not work for your architecture. I
// prefer to let the compiler handle the conversion via casting.
byteToWrite = (unsigned char) intToWrite;
write( &byteToWrite, sizeof(byteToWrite) );
// you can hard code the size, but I try to be in the habit of using sizeof
// since it is better when dealing with multibyte types
}
{
int a = 0;
unsigned char toRead = 0;
// just like the write, the byte ordering of the int will depend on your
// architecture. You could write code to explicitly handle this, but it's
// easier to let the compiler figure it out via implicit conversions
read( &toRead, sizeof(toRead) );
a = toRead;
cout<<a;
}