Reputation: 811
I have an unknown number of int variables in a text file, all i know is that the number of variables will be a multiple of 6.
I want to read these files in to a container, and split them down into smaller containers, where each container takes 6 values.
For example, if there is 30 variables in the text file, I want 5 containers, each containing 6 of the variables.And they need to be in the order that they are in the file, i.e., the first container holds the first six values.
I have read the files into a list, and vector for the moment, and was wandering which one is more suited. I've read about the split function, but after a look around, I haven't been able to successfully apply it to one of these attempts, mainly because I don't know how many smaller containers I am going to need
This is my code so far:
Vector Method
std::vector<int> Ticket;
std::ifstream fin (username + "Ticket.txt");
while (!fin.eof())
{
fin >> num;
Ticket.push_back(num);
}
fin.close();
Ticket.shrink_to_fit();
List method
std::list<int> Ticket1;
std::ifstream fin (username + "Ticket.txt");
while (!fin.eof())
{
fin >> num;
Ticket1.push_back(num);
}
fin.close();
Alternatively, if I could somehow read 6 values from the file straight into smaller containers, and keep doing this till the end of the file, hence skipping the big container that would be awesome.
Upvotes: 0
Views: 677
Reputation: 12705
You can use a vector of vectors.
std::vector< std::vector<int> > Ticket;
std::vector<int> newVector;
std::ifstream fin (username + "Ticket.txt");
while ( fin >> num )
{
newVector.push_back( num );
// if the vector is full, then insert it and start afresh
if ( newVector.size() == 6 ) {
Ticket.push_back( newVector );
newVector.clear();
}
}
fin.close();
At the end, the Ticket vector shall contain all the required vectors.
To print each vector to console, use two nested loops:
for ( int i = 0; i < Ticket.size(); i++ ) { // outer vector
for ( int j = 0; j < Ticket[i].size(); j++ ) { // inner vectors
std::cout << Ticket[i][j] << " ";
}
std::cout << "\n";
}
Upvotes: 2
Reputation: 1451
Do you need the values within the container to be contiguous as they are within a c-array, or do you need to be able to have more control over the memory allocation using the reserve member function of std::vector? If not, then consider deque or list over vector. http://www.cplusplus.com/reference/deque/deque/
You also use a fixed size array if you have a C++ 11 compiler that supports it. http://www.cplusplus.com/reference/array/array/
For instance, you could setup a container like this to setup a list of arrays.
std::list<std::array<int, 6>> containersOfSix;
On the other hand, you might be able to use one of the inserter functions if you would prefer both parts of the container to be dynamic.
http://www.cplusplus.com/reference/iterator/istream_iterator/
http://www.cplusplus.com/reference/iterator/insert_iterator/
Consider reading through that material, and toying around with some of those options. That might lead you to search for other threads related to those, which will help you find more specific examples.
Upvotes: 1
Reputation: 5083
If you create a temporary accumulator, and your ultimate destination:
vector<int> temp ;
vector<vector<int>> package ;
Then just fill up temp
, until its full, and push onto the end of package
:
while ( fin >> num )
{
temp.push_back( num) ;
if ( temp.size() == MyDesiredSize ) { package.push_back( temp) ; temp.clear() ; }
}
When you push_back
on package
it will make a copy, that's why you can clear temp
afterwards.
Upvotes: 1
Reputation: 2983
i would use the C function Fscanf()
You can use a format and read the 6 numbers at once.
fscanf(FILE,"%i %i %i %i %i %i",&var1,&var2,&var3,&var4,&var5,&var6)
Then you just need to handle the container, creating one before and reading directly to it or by coping the values if they are what you expect.
Upvotes: 0