C ++二进制文件I / O从容器中使用STL算法(不是char *等)/ [英] C++ binary file I/O to/from containers (other than char *) using STL algorithms

查看:134
本文介绍了C ++二进制文件I / O从容器中使用STL算法(不是char *等)/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图二进制文件的一个简单的测试I / O使用STL复制算法,数据/从容器和二进制文件复制。见下图:

I'm attempting a simple test of binary file I/O using the STL copy algorithm to copy data to/from containers and a binary file. See below:

 1 #include <iostream>
 2 #include <iterator>
 3 #include <fstream>
 4 #include <vector>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8
 9 typedef std::ostream_iterator<double> oi_t;
10 typedef std::istream_iterator<double> ii_t;
11 
12 int main () {
13
14   // generate some data to test
15   std::vector<double> vd;
16   for (int i = 0; i < 20; i++)
17   {
18     double d = rand() / 1000000.0;
19     vd.push_back(d);
20   }
21 
22   // perform output to a binary file
23   ofstream output ("temp.bin", ios::binary);
24   copy (vd.begin(), vd.end(), oi_t(output, (char *)NULL));
25   output.close();
26 
27   // input from the binary file to a container
28   std::vector<double> vi;
29   ifstream input ("temp.bin", ios::binary);
30   ii_t ii(input);
31   copy (ii, ii_t(), back_inserter(vi));
32   input.close();
33 
34   // output data to screen to verify/compare the results
35   for (int i = 0; i < vd.size(); i++)
36     printf ("%8.4f  %8.4f\n", vd[i], vi[i]);
37 
38   printf ("vd.size() = %d\tvi.size() = %d\n", vd.size(), vi.size());
39   return 0;
40 }

将所得的输出是如下并有两个问题,AFAIK:

The resulting output is as follows and has two problems, afaik:

1804.2894  1804.2985
846.9309    0.9312
1681.6928    0.6917
1714.6369    0.6420
1957.7478    0.7542
424.2383    0.2387
719.8854    0.8852
1649.7605    0.7660
596.5166    0.5171
1189.6414    0.6410
1025.2024    0.2135
1350.4900    0.4978
783.3687    0.3691
1102.5201    0.5220
2044.8978    0.9197
1967.5139    0.5114
1365.1805    0.1815
1540.3834    0.3830
304.0892    0.0891
1303.4557    0.4600
vd.size() = 20  vi.size() = 20

1)每双击从二进制数据读取丢失小数位前的信息。
2)数据在第三位小数错位(或更早),并正在引进一些任意的错误。

1) Every double read from the binary data is missing the information before the decimal place. 2) The data is mangled at the 3rd decimal place (or earlier) and some arbitrary error is being introduced.

请任何帮助将是AP preciated。 (我很想有人点我到previous张贴关于这一点,因为我想出总之在我搜索)

Please any help would be appreciated. (I would love for someone to point me to a previous post about this, as I've come up short in my search)

推荐答案

有关1)你需要指定一个分隔符(例如空格)的问题。非小数部分被粘到previous数的小数部分。铸造并使用NULL一般是错误的C ++。本来应该是一个暗示;)

For the question 1) You need to specify a separator (for example a space). The non-decimal part was stuck to the decimal part of the previous number. Casting and using NULL is generally wrong in C++. Should have been a hint ;)

copy (vd.begin(), vd.end(), oi_t(output, " "));

有关问题2)

#include <iomanip>
output << setprecision(9);

这篇关于C ++二进制文件I / O从容器中使用STL算法(不是char *等)/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆