2013-03-22 117 views
0

现在我有一个C++程序,在两列数据从文本文件使用类似二进制数据

while(!file.eof()) 
{ 
    double a, b; 
    file >> a >> b; // extracts 2 floating point values separated by whitespace 

    // do something with them 
} 

现在我想调整这个代码的两列读取读取列读来自.bin文件的二进制数据。我仍然希望在我的程序的其余部分将这些值视为双打。什么是最简单的方法来完成这一点?

编辑:

我写这样的二进制数据的Python程序。我认为它分为两列。

import struct 
c = struct.Struct('=ff') 
with open('numbers.bin', 'w+') as outf: 
    for r, k in nonzero: 
     outf.write(c.pack(r, k)) 
+1

http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – chris 2013-03-22 00:52:50

+1

你是什么意思的二进制两列数据?两项? 'fread' 4个字节两次 – 2013-03-22 01:07:09

回答

1

说你想读两个'列'的二进制数据没有意义。

列是人眼可视化数据的人工产物。当你读/写二进制数据时,不要考虑数据如何看待人。想象一下连续存储的硬盘上的1和0大段。忘掉一些行...忘掉一些行...等等。

你需要使用fstream'read'和'write'命令。查看fstream的这个链接,文档:http://www.cplusplus.com/reference/fstream/fstream/