0

// The array values are set after few calculations

 long int a1[356];
 long int b2[356];
 long int c3[356];

// writin to binary file

ofstream output("h.h",ios::binary|ios::out|ios::app);

output.write((char*)a1,356*4);
output.write((char*)b2,356*4);
output.write((char*)c3,356*4);

// read as

long int *x=new long int[1424];
ifstream in("h.h",ios::binary|ios::in);

in.read((char*)x,1424);

vector<long int> e1;
vector<long int> e2;
vector<long int> e3;

for(int y=;y<=1424;y=y+4)
{
 // e1.push_back((int)x[i]);

}

I am using the above method to read the integers written to the binary by I am not able to retrieve the values as stored . . can someone point where is the problem ?

Thanks!

4
  • 1
    Remember that long int may actually have different size than 4. Use sizeof(long int). Commented Feb 20, 2013 at 10:16
  • U are using ios::app which appends data, are you sure you are not reading older data? Commented Feb 20, 2013 at 10:21
  • I am sure about why I wanted to ios::app. Commented Feb 20, 2013 at 10:26
  • I used it only after checking the size - @ Joachim Commented Feb 20, 2013 at 10:27

1 Answer 1

1

With

long int *x=new long int[1424];

You declare a pointer pointing to an array of 1424 long int. But with the reading

in.read((char*)x,1424);

You are reading 1424 bytes.

And in the loop you loop over the array, but add 4 to the index, so only every four element of x is pushed back.


You should first think about that may long is eight bytes on 64-bit machines. You also don't really need to allocate the array dynamically. And the array should contain 356 entries, just like when you wrote it to the file. You can then read sizeof(x) bytes in the call to read. And finally in the loop, you loop from 0 to sizeof(x) / sizeof(x[0]):

long int x[356];
ifstream in("h.h",ios::binary|ios::in);

in.read((char*) x, sizeof(x));

for(int y = 0; y < (sizeof(x) / sizeof(x[0])); y++)
{
}

If you want to read the contents as long int into a std::vector, there are actually a "simpler" way, using iterators and standard C++ algorithms:

std::vector<long int> e1;

std::ifstream in("h.h", std::ios::binary | std::ios::in);

std::copy_n(std::istream_iterator<long int>(in),
            356,
            std::back_inserter(e1));

The above statements declares a vector, opens a file, and copies 356 long int entries from the file into the vector e1.

For references, see std::istream_iterator, std::back_inserter, and of course std::copy_n.


The above C++ solution wasn't working, so I've experimented with another:

std::vector<long> v(356);

std::ifstream in("h.h", std::ios::binary);

std::for_each(std::begin(v), std::end(v), [&in](long& v) {
    in.read(reinterpret_cast<char*>(&v), sizeof(v));
});

The above solution have been tested with this program, and it works for me.

What the above statements to, is create a vector containing 356 long entries, opens the file, and reads into the vector one entry at a time.

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks. I realized that before but couldn't sort out a way to consider other three bytes as well. Any suggestions ?
@user2090617 I've updated my answer a couple of times, once with a more "naive" C-ish solution, and once with a more idiomatic pure C++ solution.
I tried using copy_n , the test returned garbage values @ Joachim
@user2090617 Are you sure you write correct data? Do you write and read the file on the same computer?
Yes, I am sure about them !
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.