Reading ints from ifstream to a vector

D

Dr. Len

Hi all!
Given that I have a binary file which contains 4-byte integer values
in sequence and I know in advance how many there are, does the C++
standard have any algorithm or like method to read them into a
std::vector container with a single command? Right now I'm using a
temporary variable and push_back(), but this is bit clumsy way IMO:

int tmp;
std::vector<int> someVector;
std::ifstream in("somefile", std::ios_base::in |
std::ios_base::binary);

for(int i = 0; i < count; i++)
{
in.read((char*)&tmp, 4);
someVector.push_back(tmp);
}
 
J

John Harrison

Dr. Len said:
Hi all!
Given that I have a binary file which contains 4-byte integer values
in sequence and I know in advance how many there are, does the C++
standard have any algorithm or like method to read them into a
std::vector container with a single command? Right now I'm using a
temporary variable and push_back(), but this is bit clumsy way IMO:

int tmp;
std::vector<int> someVector;
std::ifstream in("somefile", std::ios_base::in |
std::ios_base::binary);

for(int i = 0; i < count; i++)
{
in.read((char*)&tmp, 4);
someVector.push_back(tmp);
}

std::vector<int> someVector(count);
std::ifstream in("somefile", std::ios_base::in | std::ios_base::binary);
in.read((char*)&someVector[0], sizeof(int)*count);

john
 
J

John Harrison

John Harrison said:
Dr. Len said:
Hi all!
Given that I have a binary file which contains 4-byte integer values
in sequence and I know in advance how many there are, does the C++
standard have any algorithm or like method to read them into a
std::vector container with a single command? Right now I'm using a
temporary variable and push_back(), but this is bit clumsy way IMO:

int tmp;
std::vector<int> someVector;
std::ifstream in("somefile", std::ios_base::in |
std::ios_base::binary);

for(int i = 0; i < count; i++)
{
in.read((char*)&tmp, 4);
someVector.push_back(tmp);
}

std::vector<int> someVector(count);
std::ifstream in("somefile", std::ios_base::in | std::ios_base::binary);
in.read((char*)&someVector[0], sizeof(int)*count);

john

BTW the above code fails if count is zero, should really be

std::vector<int> someVector(count);
if (count > 0)
{
std::ifstream in("somefile", std::ios_base::in | std::ios_base::binary);
in.read((char*)&someVector[0], sizeof(int)*count);
}

john
 
V

Victor Bazarov

Dr. Len said:
Given that I have a binary file which contains 4-byte integer values
in sequence and I know in advance how many there are, does the C++
standard have any algorithm or like method to read them into a
std::vector container with a single command? Right now I'm using a
temporary variable and push_back(), but this is bit clumsy way IMO:

int tmp;
std::vector<int> someVector;
std::ifstream in("somefile", std::ios_base::in |
std::ios_base::binary);

for(int i = 0; i < count; i++)
{
in.read((char*)&tmp, 4);
someVector.push_back(tmp);
}


someVector.resize(count);
in.read((char*)&someVector[0], sizeof(int)*count);

Victor
 
A

Alex Vinokur

Dr. Len said:
Hi all!
Given that I have a binary file which contains 4-byte integer values
in sequence and I know in advance how many there are, does the C++
standard have any algorithm or like method to read them into a
std::vector container with a single command? Right now I'm using a
temporary variable and push_back(), but this is bit clumsy way IMO:

int tmp;
std::vector<int> someVector;
std::ifstream in("somefile", std::ios_base::in |
std::ios_base::binary);

for(int i = 0; i < count; i++)
{
in.read((char*)&tmp, 4);
someVector.push_back(tmp);
}

------ file2vect.cpp : BEGIN ------
#include <cstdio>
#include <cassert>
#include <vector>
#include <iostream>
#include <iterator>
#include <fstream>
using namespace std;

vector<int> read_file_to_vector(const char* const filename_i)
{
ifstream f(filename_i, ios_base::binary);
assert (f);

istream_iterator<int> b(f), e;
const vector<int> v (b, e);

return v;
}

void create_test_infile (const char* const filename_i)
{
::remove (filename_i);
ofstream outfile (filename_i, ios_base::binary);
assert (outfile);

outfile << 1234 << " " << 5678 << " " << 9012 << endl;
}

int main ()
{
#define INFILE_NAME "foo.in"

create_test_infile (INFILE_NAME);
const vector<int> v (read_file_to_vector (INFILE_NAME));

// ------ Show ------
ifstream infile(INFILE_NAME, ios_base::binary);
assert (infile);

cout << "File : ";
cout << infile.rdbuf();

cout << "Vector : ";
copy (v.begin(), v.end(), ostream_iterator<int> (cout, " "));
cout << endl;
// ------------------

return 0;
}
------ file2vect.cpp : END --------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top