读取和转换C+中的二进制数据+; 我是一个新的C++程序员,负责数据收集。我有这个代码来读取和打印数据:< /P> int main () { string name; ifstream input; double vector_length; vector_length = 14; //Open the input file input.open("data.txt",ios::out | ios::app | ios::binary ); vector<complex<double> > f(vector_length); for(int i=0; i<vector_length;i++){ getline (input,name); cout << name << endl; istringstream is(name); complex<double> c; is >> c; f[i] = complex<double>(c.real(), c.imag()); cout << "the complex number is " << c << "\n"; cout <<"real: " << c.real() << endl; cout <<"image:" << c.imag() << endl; } for(int i=0; i < vector_length;i++){ cout <<i<<" "<< f[i]<<endl; } return 0; }

读取和转换C+中的二进制数据+; 我是一个新的C++程序员,负责数据收集。我有这个代码来读取和打印数据:< /P> int main () { string name; ifstream input; double vector_length; vector_length = 14; //Open the input file input.open("data.txt",ios::out | ios::app | ios::binary ); vector<complex<double> > f(vector_length); for(int i=0; i<vector_length;i++){ getline (input,name); cout << name << endl; istringstream is(name); complex<double> c; is >> c; f[i] = complex<double>(c.real(), c.imag()); cout << "the complex number is " << c << "\n"; cout <<"real: " << c.real() << endl; cout <<"image:" << c.imag() << endl; } for(int i=0; i < vector_length;i++){ cout <<i<<" "<< f[i]<<endl; } return 0; },c++,C++,现在,我想用同样的方法,但是从二进制数据中获取值(input.open(“data.bin”,ios::out | ios::app | ios::binary);)例如: 004038c0 e5 ff 32 00 e1 ff 13 00 00 00 00 00 f9 ff b0 ff |..2.............| 004038d0 e8 ff b2 ff de ff f3 ff c0 ff 06 00 b2 ff 18 00 |................| 00

现在,我想用同样的方法,但是从二进制数据
中获取值(input.open(“data.bin”,ios::out | ios::app | ios::binary);)
例如:

004038c0  e5 ff 32 00 e1 ff 13 00  00 00 00 00 f9 ff b0 ff  |..2.............| 
004038d0  e8 ff b2 ff de ff f3 ff  c0 ff 06 00 b2 ff 18 00  |................|  
004038e0  41 00 09 00 29 00 bc ff  9f ff f9 ff f8 ff 51 00  |A...).........Q.|
004038f0  0d 00 de ff b1 ff 18 00  c4 ff 26 00 19 00 bd ff  |..........&.....|
00403900  10 00 fe ff 14 00 2e 00  d0 ff 05 00 8f ff fa ff  |................|
00403910  44 00 2a 00 3b 00 c9 ff  ed ff e2 ff f3 ff 21 00  |D.*.;.........!.|
00403920  0e 00 ef ff af ff f8 ff  bf ff f6 ff 0b 00 ce ff  |................|
00403930  03 00 20 00 11 00 0e 00  02 00 6f ff c9 ff c5 ff  |.. .......o.....|
00403940  cf ff f5 ff d5 ff 94 ff  d6 ff a3 ff 25 00 2c 00  |............%.,.|

表示上面的复杂数据。
(16,48)(-80,80)(-48128)
…但我无法打印数据。

使用标志
ios::binary
打开
std::ifstream
,这一事实只会影响操作系统处理读取的方式。例如,在MS Windows操作系统上,对于文本文件而不是二进制文件,将“\n\r”序列转换为“\r”,并将“ctrl-z”转换为EOF。但该标志不会更改
运算符>>
以加载二进制数据。您必须使用
std::ifstream::read()
方法将二进制数据读入内存:

double real = 0, imag = 0;
input.read( reinterpret_cast<char *>( &real ), sizeof( real ) );
input.read( reinterpret_cast<char *>( &imag ), sizeof( imag ) );
complex<double> c( real, imag );
double real=0,imag=0;
input.read(reinterpret_cast(&real)、sizeof(real));
input.read(reinterpret_cast(&imag),sizeof(imag));
复合物c(real,imag);

您是否尝试过任何可以读取二进制文件的方法?如果是这样的话,你能把它贴出来让我们给你指出正确的方向吗?
f[i]=complex(c.real(),c.imag())为什么需要这个?出现了什么错误?产生错误的代码是什么?您使用了什么数据,显示的数据?请发布一条消息。
>
输入操作符用于文本格式的输入。UkMonkey:我刚试过,但它什么也没给我,或者打印出来:
�P����������0P��?��P������@��������O���<代码>�0����_P�0����P���0@�P�P�����P�P������P�0� ���P�0��P���op��@�������������P���?����@P��<代码>�������� P����� �@`��@��@����P^C…………但我想知道实数。鉴于OP没有发布任何相关代码,目前唯一的线索是打印数据不起作用,我认为您还不能认为读取文件是错误的。@据我所知,OP使用的代码相同,刚刚在
std::ifstream
中添加了
ios::binary
标志,但是coutSlava:它有点像是数据的第一个只读值(例如上面的:(16,-144)数据)。那么,如果我想打印所有数据,最好的方法是什么?谢谢。@Slava它是数据的第一个只读值(例如上面的:(16,-144)数据)。那么,如果我想打印所有数据,最好的方法是什么?非常感谢。
double real = 0, imag = 0;
input.read( reinterpret_cast<char *>( &real ), sizeof( real ) );
input.read( reinterpret_cast<char *>( &imag ), sizeof( imag ) );
complex<double> c( real, imag );