0

I need your help again for the save and read data in binary. I have, a vector<<complex> > xy(256) which is read from the hardware in 10 times:

vector<<complex> > xy(256);
ofstream outfile2 (outfilename2.c_str() , ofstream::binary);
....
....
for(unsigned t = 0; t < 10; t++)
{
     ....
     ....
     for(unsigned i = 0; i < 256; i++)
     {
           xy[i] = f[i] * conj(g[i]);
     }
     for(unsigned i = 0; i < 256; i++)
     {
           outfile2 << boost::format("%20.8e") % xy[i]<< endl;  // write in text
     }
}  // the text data will be 2560 lines of complex data, for example:
   // (6.69635350e+06,7.34146150e+06)

Now, I am trying to save into binary file, using this command:

for(unsigned i = 0; i < 256; i++)
     {
        outfile2.write((const char*)& xy[i], 1 * sizeof(complex<short>));
        outfile2.flush();
     }

Although, it still give me a data, but when i compare to the original text data, they were different. I do not understand why?

I would like to read complex 16 with floating point data.

I hope you guys can help.

Thank you very much.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
long khong
  • 73
  • 1
  • 2
  • 9
  • @Sergey Sergey I hope you can help me on this question, i am still confusing on the binary data. Thank you very much. – long khong Nov 22 '16 at 17:11
  • 2
    *"when i compare to the original text data, they were different"* different how? please post example output (plus what you expected). also how are you comparing it? – UnholySheep Nov 22 '16 at 17:27
  • &xy[i] maybe have sense, maybe not. vector isn't clasic C array. and indexing operator is "emulated" – Jacek Cz Nov 22 '16 at 17:28
  • @UnholySheep this is an output: (-28153,20316) (23951,20299)(-23260,20281) (10184,20268) (12796,20245) with 2560 lines. and I am expecting the complex data have to be : (1.00663576e+08,2.21873700e+07) (1.96770960e+07,7.86981300e+06) (1.48334580e+07,1.66604570e+07) (4.34163400e+07,3.11434200e+07) (3.44546400e+07,3.15276640e+07) – long khong Nov 22 '16 at 18:06
  • @UnholySheep and the code I am trying to read binary data: ifstream input(filename.c_str() , std::ios::binary | std::ios::in); vector > vector(vector_size); input.seekg(0, ios::beg); input.read(reinterpret_cast(vector.data()), (2650 * sizeof(complex))); – long khong Nov 22 '16 at 18:11
  • @JacekCz I do not think &xy[i] causing problem, I guess it should be a data type, I had a suggestion using read complex 16 with floating point data, but I did not know how to do it. – long khong Nov 22 '16 at 18:16
  • @n.m. what is your question? anything wrong with the vector< >? – long khong Nov 22 '16 at 19:22
  • @longkhong it's not legal C++. this is what's wrong with it. – n. m. could be an AI Nov 22 '16 at 20:05
  • @UnholySheep do you have any ideal? – long khong Nov 23 '16 at 14:57
  • @JacekCz do you have any ideal? – long khong Nov 23 '16 at 14:57
  • @m.n. I am sorry, I do not understand when you said It is now legal in C++, but I still can have data in text file, could you please give me more details? – long khong Nov 23 '16 at 14:58

1 Answers1

0

I have written a demo, which works on my computer.

#include <cassert>

#include <complex>
#include <fstream>
#include <iostream>
#include <type_traits>
#include <vector>

int main() {
  assert( std::is_trivially_copy_assignable<std::complex<short> >::value );

  std::vector<std::complex<short> > vtr(256);
  for (int i=0; i<256; ++i)
    vtr[i] = std::complex<short>(i*2,i*2+1);

  {
    std::ofstream output("data.bin", std::ofstream::binary);
    for (int i=0; i<256; ++i)
      output.write((char *)&vtr[i],sizeof(vtr[i]));
  }
  vtr.clear();

  std::cout << vtr.size() << std::endl;

  vtr.resize(256);
  {
    std::ifstream input("data.bin", std::ifstream::binary);
    for (int i=0; i<256; ++i)
      input.read((char *)&vtr[i],sizeof(vtr[i]));
  }

  for (size_t i=0; i<vtr.size(); ++i)
    std::cout << vtr[i] << " ";
  std::cout << std::endl;

  return 0;
}

You could run this demo on your computer. If this demo works, it's likely that you made some mistakes in your code. Please provide a complete, and verifiable example.

felix
  • 2,213
  • 7
  • 16
  • hi felix, I am trying to use your codes, it's worked with the sample when I created vector complex (0,1)(2,3)(4,5)(6,7)(8,9).... but when I am trying to get these data: (1.11122130e+07,1.92728450e+06)(2.92449620e+07,1.50776630e+07)(9.36541200e+07,5.38220040e+07)(2.16838928e+08,1.09020568e+08)... it gave me the different number: (-18123,19347)(-28519,19272)(-14506,19421)(27752,19550)... I do not think that i collected the right data, even I use matlab to plot, they are much different. I guess the data i am trying to get is high value numbers, Do you have any ideal, Felix? – long khong Nov 23 '16 at 17:09
  • someone suggested me to try with read complex 16 or 32, but I still do not understand how to make it work, hope you can give me more ideal? thank you, Felix. – long khong Nov 23 '16 at 17:11
  • Are you trying running something like std::complex value(1e7,2e6); ? – felix Nov 23 '16 at 17:49
  • felix, yes, I am trying to run kind of those data with high values, and about 2560 lines of vector complex. – long khong Nov 23 '16 at 18:36
  • 1e7 is out of the range a short can normally hold. I suggest you use double instead of short, the former can provide 15–17 decimal digits precision, which should be enough. [double](https://en.wikipedia.org/wiki/Double-precision_floating-point_format), [value range](http://stackoverflow.com/questions/1819189/what-range-of-values-can-integer-types-store-in-c) – felix Nov 23 '16 at 19:23
  • A **rough** range for different arithmetic types: char(+-100), short(+-30000), long int(+-0.2 billion), long long int (very big), float(+-1e38), double(+-1e308). – felix Nov 23 '16 at 19:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128871/discussion-between-felix-and-long-khong). – felix Nov 23 '16 at 19:47
  • hi felix, sorry for the late response because of Thanks Giving day, I hope you are still available, so I can chat with you right now. – long khong Nov 25 '16 at 14:38
  • It's alright. If you have any question, just leave a comment or a message in the chat channel. – felix Nov 25 '16 at 15:11
  • If I want to read this binary data in Matab, I can do this way: filename = 'data.bin'; fid = fopen(filename); fseek(fid,1,'bof'); y = fread(fid,[2,inf],'long'); x = complex(y(1,:),y(2,:)); figure, plot(abs(x)); but when I compare between matlab and C++ data, also plot it out, I have the different plot. Do you know what reason? – long khong Nov 25 '16 at 16:34
  • Reading binary files written by another program can be hard, I suggest you start with simple samples, a file contains only 2 or 3 long int. You read the data, store them in a buffer, and study their binary structure. Or you can figure out how to read them by studying some relative documents that are provided by the provider of the binary file. – felix Nov 25 '16 at 16:49
  • I am not familiar with matlab, giving me the matlab code doesn't help a lot. But if possible, I could use some data samples for helping you. You can share them by dropbox links or github. – felix Nov 25 '16 at 16:54