How to output array of doubles to hard drive?

前端 未结 6 515
野的像风
野的像风 2020-12-09 06:42

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

相关标签:
6条回答
  • 2020-12-09 06:55

    Now I feel old. I asked this question a long time ago (except about ints). comp.lang.c++ link

    0 讨论(0)
  • 2020-12-09 06:57
    #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/

    Just set theArray and arrayLength to whatever your code requires.

    0 讨论(0)
  • 2020-12-09 07:01

    You can use iostream .read() and .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. 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.

    0 讨论(0)
  • 2020-12-09 07:06

    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.

    0 讨论(0)
  • 2020-12-09 07:17

    Use std::copy() with the stream iterators. 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.
                     );
        }
    }
    
    0 讨论(0)
  • 2020-12-09 07:18
    #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. You can use multithreading to not blocking system wile saving data

    #include <fstream>
    
    using namespace std;
    
    0 讨论(0)
提交回复
热议问题