read irregular file into a 2-dimension array using C++
I am very new to C++. I believe there are solutions开发者_运维技巧 already in stackoverflow but I cannot find any.
I need to read data from a txt file into a 2 dimension array. File is like
54 3 5 678
10 1 2 3 46 8 1 1 2 3 4
9 8 10
Each line contains up to 120 integers and there are no more than 60 lines.
Your reply is high appreciated. Thanks!
Update: It is not homework. Thank you all!
Here goes...
Definitely the answer you need1, though very correct and true to C++ nature.
Instead of parsing to a jagged array it reads into a vector
of vector
s of int
s.
(Or, a list of sets, a stack of deques, whatever tickles your fancy).
In absense of any specs, I accept
- any number of lines (until the first empty one),
- any number (>0) of numbers on any line
- ignoring whitespace anywhere (besides the line ends)
- any integer numbers (including negatives...)
Cheers
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_istream_iterator.hpp>
#include <boost/spirit/include/karma.hpp>
namespace spirit = boost::spirit;
namespace qi = boost::spirit::qi;
namespace karma = boost::spirit::karma;
int main()
{
std::cin.unsetf(std::ios::skipws);
spirit::istream_iterator b(std::cin), e;
std::vector<std::vector<int> > vectors;
if (qi::phrase_parse(b, e, +qi::int_ % qi::eol, qi::space - qi::eol, vectors))
std::cout
<< karma::format(karma::right_align(8)[karma::auto_] % ',' % '\n', vectors)
<< std::endl;
return 0;
}
For the input shown, it prints:
54, 3, 5, 678
10, 1, 2, 3, 46, 8, 1, 1, 2, 3, 4
9, 8, 10
Update Standard Library version
See it live here: http://ideone.com/HtAAg
For fun, here is the standard-library-only version of the same. Just to show, that things don't have to be all that bad when you go without the jet propulsion libraries :)
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <iterator>
static void display(const std::vector<int>& v)
{
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, ",\t"));
std::cout << std::endl;
}
int main()
{
std::vector<std::vector<int> > vectors;
std::string s;
while (std::getline(std::cin, s))
{
std::istringstream iss(s);
vectors.push_back(std::vector<int>());
std::copy(std::istream_iterator<int>(iss), std::istream_iterator<int>(), std::back_inserter(vectors.back()));
}
std::for_each(vectors.begin(), vectors.end(), display);
return 0;
}
1 I'm assuming that this is homework. I'm also assuming that your teacher wants you to allocate a int[120][60]
or int[60][120]
(how would you know how many numbers were read per line? - you'd need extra storage)
My solution, just substitute cin with your file stream
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(int argc, char* argv[]) {
string line;
int ints[60][120] = {0}, i = 0;
stringstream ss;
while (getline(cin, line)) {
ss.str(line);
for(int j = 0; j < 120 && ss; j++) {
ss >> ints[i][j];
}
ss.clear();
i++;
}
cin.ignore();
return 0;
}
精彩评论