简体   繁体   English

如何输出双打数组到硬盘?

[英]How to output array of doubles to hard drive?

I would like to know how to output an array of doubles to the hard drive. 我想知道如何输出一个双打数组到硬盘驱动器。

edit: 编辑:

for further clarification. 进一步澄清。 I would like to output it to a file on the hard drive (I/O functions). 我想将它输出到硬盘驱动器上的文件(I / O功能)。 Preferably in a file format that can be quickly translated back into an array of doubles in another program. 优选地,文件格式可以快速转换回另一个程序中的双打数组。 It would also be nice if it was stored in a standard 4 byte configuration so that i can look at it through a hex viewer and see the actual values. 如果它存储在标准的4字节配置中也是很好的,这样我就可以通过十六进制查看器查看它并查看实际值。

Hey... so you want to do it in a single write/read, well its not too hard, the following code should work fine, maybe need some extra error checking but the trial case was successful: 嘿......所以你想在一次写/读中做到这一点,这不是太难,以下代码应该可以正常工作,可能需要一些额外的错误检查,但试用案例是成功的:

#include <string>
#include <fstream>
#include <iostream>

bool saveArray( const double* pdata, size_t length, const std::string& file_path )
{
    std::ofstream os(file_path.c_str(), std::ios::binary | std::ios::out);
    if ( !os.is_open() )
        return false;
    os.write(reinterpret_cast<const char*>(pdata), std::streamsize(length*sizeof(double)));
    os.close();
    return true;
}

bool loadArray( double* pdata, size_t length, const std::string& file_path)
{
    std::ifstream is(file_path.c_str(), std::ios::binary | std::ios::in);
    if ( !is.is_open() )
        return false;
    is.read(reinterpret_cast<char*>(pdata), std::streamsize(length*sizeof(double)));
    is.close();
    return true;
}

int main()
{
    double* pDbl = new double[1000];
    int i;
    for (i=0 ; i<1000 ; i++)
        pDbl[i] = double(rand());

    saveArray(pDbl,1000,"test.txt");

    double* pDblFromFile = new double[1000];
    loadArray(pDblFromFile, 1000, "test.txt");

    for (i=0 ; i<1000 ; i++)
    {
        if ( pDbl[i] != pDblFromFile[i] )
        {
            std::cout << "error, loaded data not the same!\n";
            break;
        }
    }
    if ( i==1000 )
        std::cout << "success!\n";

    delete [] pDbl;
    delete [] pDblFromFile;

    return 0;
}

Just make sure you allocate appropriate buffers! 只需确保分配适当的缓冲区! But thats a whole nother topic. 但那是一个完整的话题。

You can use iostream .read() and .write(). 您可以使用iostream .read()和.write()。

It works (very roughly!) like this: 它的工作原理(非常粗略!)如下:

double d[2048];
fill(d, d+2048, 0);
ofstream outfile ("save.bin", ios::binary);
outfile.write(reinterpret_cast<char*>(&d), sizeof(d));

ifstream infile ("save.bin", ios::binary);
infile.read(reinterpret_cast<char*>(&d), sizeof(d));

Note that this is not portable between CPU architectures. 请注意,这在CPU体系结构之间不可移植 Some may have different sizes of double. 有些可能有不同大小的双倍。 Some may store the bytes in a different order . 有些可能以不同的顺序存储字节。 It shouldn't be used for data files that move between machines or data that is sent over the network. 它不应该用于在机器之间移动的数据文件或通过网络发送的数据。

Use std::copy() with the stream iterators. 将std :: copy()与流迭代器一起使用。 This way if you change 'data' into another type the alterations to code would be trivial. 这样,如果将“数据”更改为其他类型,则代码的更改将是微不足道的。

#include <algorithm>
#include <iterator>
#include <fstream>

int main()
{
    double         data[1000] = {/*Init Array */};

    {
        // Write data too a file.
        std::ofstream   outfile("data");
        std::copy(data,
                  data+1000,
                  std::ostream_iterator<double>(outfile," ")
                 );
    }

    {
        // Read data from a file
        std::ifstream    infile("data");
        std::copy(std::istream_iterator<double>(infile),
                  std::istream_iterator<double>(),
                  data // Assuming data is large enough.
                 );
    }
}
#include <fstream.h>

void saveArray(double* array, int length);

int main()
{
    double array[] = { 15.25, 15.2516, 84.168, 84356};
    saveArray(array, 4);

    return 0;
}

void saveArray(double* array, int length)
{
    ofstream output("output.txt");

    for(int i=0;i<length;i++)
    {
        output<<array[i]<<endl;
    }
}

here is a way to output an array of doubles to text file one per line. 这是一种将每行一个双打数组输出到文本文件的方法。 hope this helps 希望这可以帮助
EDIT 编辑
Change top one line to this two, and it will compile in VS. 将前一行更改为这两行,它将在VS中编译。 You can use multithreading to not blocking system wile saving data 您可以使用多线程来阻止系统保存数据

#include <fstream>

using namespace std;

Now I feel old. 现在我觉得自己老了。 I asked this question a long time ago (except about ints). 我很久以前就问了这个问题(除了关于整数)。 comp.lang.c++ link comp.lang.c ++链接

#include <iostream>
#include <fstream>

using namespace std;

int main () {
  double [] theArray=...;
  int arrayLength=...;
  ofstream myfile;
  myfile.open ("example.txt");
  for(int i=0; i<arrayLength; i++) {
    myfile << theArray[i]<<"\n";
  }
  myfile.close();
  return 0;
}

adapted from http://www.cplusplus.com/doc/tutorial/files/ 改编自http://www.cplusplus.com/doc/tutorial/files/

Just set theArray and arrayLength to whatever your code requires. 只需将theArray和arrayLength设置为您的代码所需的任何内容即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM