views:

150

answers:

2

Hi guys, Know this might be rather basic, but I been trying to figure out how to one after create a dynamic array such as

double* data = new double[size];

be used as a source of data to be kept in to a binary file such as

ofstream fs("data.bin",ios:binary");
fs.write(reinterpret_cast<const char *> (data),size*sizeof(double));

When I finish writing, I attempt to read the file through

double* data = new double[size];
ifstream fs("data.bin",ios:binary");
fs.read(reinterpret_cast<char*> (data),size*sizeof(double));

However I seem to encounter a run time error when reading the data. Do you guys have any advice how i should attempt to write a dynamic array using pointers passed from other methods to be stored in binary files?

A: 
  1. Hy:)
If you use Unix you have some functions:    int read(int fd, void

*buf, int count);. int write (int fd, void *buf, int nbytes);.

The functions are verry fast because
are kernel-functions.

i run your code and everything was
ok : 

      1 #include <iostream>
      2 #include <fstream>
      3 #include <cstring>
      4 using namespace std;
      5 
      6 int main()
      7 {
      8     double * s;
      9     int ns = 10;                    //information
     10     s = new double[ns];
     11 
     12     for (int i = 0 ; i < ns ; ++i)
     13         s[i] = 17*i + 23;           // i put my information here
     14 
     15     ofstream os("data.txt", ios::binary);
     16     os.write(reinterpret_cast<const char*> (s), sizeof(double) * ns);
     17     os.close();
     18 
     19     double * final;
     20     final = new double[ns];
     21 
     22     ifstream is("data.txt", ios::binary);
     23     is.read(reinterpret_cast<char *>(final), sizeof(double) * ns);
     24     is.close();
     25 
     26     for (int i=0; i < ns; ++i)
     27         cout<<s[i]<<" "<<final[i]<<"\n";
     28 
     29     return 0;
     30 }

~            
none@Brusture:~/Desktop$ g++ -o test
test.cpp  none@Brusture:~/Desktop$
./test  23 23 40 40 57 57 74 74 91
91 108 108 125 125 142 142 159 159
176 176
serbanlupu
Thanks for taking the effort and time to test out my problem. It really did seem to run well now, the problem was some where else that lead to the error to the persistent method.Thanks again serbanlupu.
Yijinsei
+2  A: 

Streams are not the only nor always the best way to access binary files. This is another way that uses the virtual memory mechanisms of the operating system (i.e. the same thing that make your memory swap to disk):

  template<class T>
    struct BinaryData{
    BinaryData(std::string const& fname,unsigned size):_size(size) {
       int fd=open(fname.c_str(),O_RDWR,O_CREAT);
       //if modifying to different size or creating
       ftruncate(fd,size*sizeof(T));
       //get the memory
       _data=reinterpret_cast<T*>(mmap(0,size*sizeof(T),PROT_WRITE|PROT_READ,MAP_SHARED,fd,0));
       fclose(fd);
    }

    ~BinaryData(){
      munmap(_data);
    }
    T* begin(){return _data; }
    T* end(){return _data+_size; }
    private:
     T* _data;
     unsigned _size;
    };

This just makes the memory swap to a differnt swap file, i.e. the one you specify. The flags chosen will always force it to write the contents to disk, either when the program exits or munmap is invoked. This example of course does not have anything in terms of error checking and the syntax only works on POSIX systems. However Windows has identical semantics in its CreateFileMapping function.

Lance Diduck
thanks for the response, i never heard of such method before. Truly eye opening, I shall look into it. Thanks!
Yijinsei