Using a vector to read an array from a binary file

I've created a vector array, by obtaining a single value from a binary file. For this particular case, this number (nx), is 200. However, unfortunately i need this number to be able to change depending on my needs.

Next, i want to get an array of numbers from the binary file, of size nx. Here is my attempt:

1
2
3
4
5
6
7
8
9
10
11
12
  
ifstream inputFile;
inputFile.open("test", ios::binary);

int nx;

inputFile.read((char *) &nx, sizeof(nx));

vector<float> x (nx);  //where nx = 200 in this particular case, and is taken from the binary file

inputFile.read((char *) &x, sizeof(x));


however, this doesn't seem to work. I assume i have to treat the vector reading differently. If i explicitly do:

 
float x[200];


then my code works. Unfortunately, i don't know quite how to proceed - so any help is appreciated.

I have a feeling i might be on this forum asking lots of questions for some time to come ;)
Last edited on
Wait, Huh? If you need a variable in a vector to change then... change it... What are you having trouble with?

Line 7 is just going to read in "sizeof int data type in bytes" it isn't going to read the number of bytes you set nx to. For that you would drop sizeof() and use nx. Also why are we declaring nx as an integer on line 5 then casting it as a char on line 7? You may have a good reason for this but I can't see what it is right now.

Line 9 is NOT how you resize vectors, for that you'll want to use resize(): http://www.cplusplus.com/reference/stl/vector/resize/. But you should be loading your vectors with push_back():http://www.cplusplus.com/reference/stl/vector/push_back/ anyway.

On Line 11 you're doing that thing from line 7 again, why?

EDIT: I figured out what you are doing on lines 7 and 11 so we can move on.
Last edited on

Wait, Huh? If you need a variable in a vector to change then... change it... What are you having trouble with?


Sorry for the confusion - this does happen already.



Line 7 is just going to read in "sizeof int data type in bytes" it isn't going to read the number of bytes you set nx to. For that you would drop sizeof() and use nx. Also why are we declaring nx as an integer on line 5 then casting it as a char on line 7? You may have a good reason for this but I can't see what it is right now.



From the vast amount of things i've read about reading in data from a binary file, this is the method that has worked for me. Perhaps it's by a bit of a fluke, but nevertheless, i get what i want. In regards changing from int to char, it seems to work....... I get the correct value for nx using this method. I've tried putting in int and float in the read statement, but to no avail. Only the char seems to work.

I'd be happy to be instructed otherwise though. I'm only very much beginning to learn how to do all this.


Line 9 is NOT how you resize vectors, for that you'll want to use resize(): http://www.cplusplus.com/reference/stl/vector/resize/. But you should be loading your vectors with push_back():http://www.cplusplus.com/reference/stl/vector/push_back/ anyway.


Ok - i see what you do with the resize. Good to know, thanks. Line 9 now becomes:

1
2
vector<float> x;
x.resize(nx);


However, I don't know how to read in the data, with read, and then the push_back......... You say that Line 7 is wrong(?) - but i get the correct answer. However, it's Line 11 that causes me the problems, unless i don't use the vector array, and just use

 
float x[200];


Hope this makes more sense.

Thanks for the help so far. Much appreciated.
Ok - i seem to have got it to work.

Please let me know though, if there are better ways / practises i can use.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

ifstream inputFile;
inputFile.open("test", ios::binary);

int nx;

inputFile.read((char *) &nx, sizeof(nx));

vector<float> x;
x.resize(new_nx);

for (int i = 0; i < nx; i++)
{
      inputFile.read((char *) &x[i], sizeof(x[i]));
}      



looks like all i needed was to put it in a loop......
Yeah when I first went through your code I forgot that read(...) returns a char, I caught on a few minutes later. Lines 7 and 11 appear to be ok except you should use push_back(...) to load vectors. In order to do this you should probably declare some variables to hold the values read in from your "inputFile".

EDIT: You seem to be stuck in this funny way of using vectors. Is there a reason you don't use ">>" to read in the stream?
Last edited on
so how would the push_back thing work?

ie. how'd you implement that into the code?
Is there a reason you don't use ">>" to read in the stream?
Because it's binary data.

vector<float> x(nx); uses the constructor to reserve create nx cells with 0 as value.

If you want to use push_back
1
2
3
4
5
6
7
x.clear(); //size=0
x.reserve( nx );
for( int i=0; i<nx; i++){
  int aux;
  inputFile.read( (char *)&aux, sizeof(aux) );
  x.push_back( aux );
}

However to read an entire block you better use
1
2
3
4
template<class T>
void read(std::ifstream &input, std::vector<T> &v){
	input.read( (char *) &v[0], sizeof(T)*v.size() );
}
(It should be an easier way)
Last edited on
Topic archived. No new replies allowed.