473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Reading ints from ifstream to a vector

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);
}

Jul 22 '05 #1
4 16901

"Dr. Len" <le*@wohoo.org> wrote in message
news:gc********************************@4ax.com...
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
Jul 22 '05 #2

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2m************@uni-berlin.de...

"Dr. Len" <le*@wohoo.org> wrote in message
news:gc********************************@4ax.com...
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
Jul 22 '05 #3
Dr. Len wrote:
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
Jul 22 '05 #4

"Dr. Len" <le*@wohoo.org> wrote in message news:gc********************************@4ax.com...
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 --------
--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Santah | last post by:
hi I'm new to C++ and I'm currently working on Visual C++ 6.0 I'm trying to open a text file, and read some data from it part of the text file looks like this: --------
1
by: Brad Marts | last post by:
I have a file containing numbers stored in binary form and information in ascii which tells me what kind of numbers they are (int, float, double, etc.). I want to be able to read in the numbers and...
18
by: Michael | last post by:
Hi, I moved to c++ from c, and wanted to know what the best way to read data from files is in c++. Any thoughts? fscanf() is possible but fairly painful! Regards Michael
2
by: Alexander Schmidt | last post by:
Hi, I am not very familiar with C++ programming, so before I do a dirty hack I ask for a more elegant solution (but only the usage of STL is allowed, no special libs). So I need to read a file...
2
by: Jason Heyes | last post by:
I want to read the binary contents of a file whose size is over 1 megabyte. I tried to use this function. bool read_file(const char *fname, std::vector<char> &data) { std::ifstream in(fname);...
7
by: fakeprogress | last post by:
For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that...
6
by: Pantheronics | last post by:
// file to read 12 13 14 1 1 2 3 1 2 3 1 ; /// The source file #include <iostream> #include <fstream>
9
by: Eric Lilja | last post by:
Hi! I have a program with a class that needs to be able to write itself to a file in clear text format. The file has two integers and vector of struct objects. The struct has a string that can...
15
by: arnuld | last post by:
This is the partial-program i wrote, as usual, i ran into problems halfway: /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a function to open a file for input and then read...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.