Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Reading an int array from file. (http://www.velocityreviews.com/forums/t622129-reading-an-int-array-from-file.html)

Compass 06-25-2008 06:12 AM

Reading an int array from file.
 
Hi all,

I have an int array in a text file. The file structure is like this:
[1, 2, 3, 4]
[5, 6, 7, 8]
[[1, 2], [3, 4], [5, 6], [7, 8]]

How can I easily read them in to three int arrays?

Thanks a lot!

Mirco Wahab 06-25-2008 08:20 AM

Re: Reading an int array from file.
 
Compass wrote:
> I have an int array in a text file. The file structure is like this:
> [1, 2, 3, 4]
> [5, 6, 7, 8]
> [[1, 2], [3, 4], [5, 6], [7, 8]]
>
> How can I easily read them in to three int arrays?


The third line looks like an array of arrays.
BTW, if it's only the numbers, you could simply
read the file line-by-line and split the line on
'non-numbers'. If you can abstract this out
into a function, your program may look like:

...

typedef std::vector<int> IntVector;

...

int main(int argc, char *argv[])
{
const char *fname = argv[1];
string line;

ifstream fh(fname);
vector<IntVector> vv;

// read the file and split the lines
while( getline(fh, line) )
vv.push_back( split("\\D+", line) );

// print out the arrays on the screen
for(size_t row=0; row<vv.size(); row++) {
for(size_t col=0; col<vv[row].size(); col++)
cout << vv[row][col] << ' ';
cout << endl;
}

return 0;
}

where argv[1] contains the name of your
data file, Your individual arrays (of type
IntVector) will contain the values.

All three arrays are collected in the array
vv (vv[0], vv[1], vv[2]) and are produced by
a function 'split(...)'. This function requires
you to provide a pattern *where* to split the
numbers. In regular-expressionese, this is,
if only Integers are considered, simply a "\D"
(which means: all characters that are no digits).

How would the split function look like? It should
return one IntVector for each line:

IntVector split(const char *pattern, const string& line)
{
IntVector v;
boost::regex num(pattern);
boost::sregex_token_iterator p(line.begin(), line.end(), num, -1), q;
while(p != q) {
string s = (*p++).str();
if(s.length()) v.push_back( atoi(s.c_str()) );
}
return v;
}

and employs the 'sregex_token_iterator' which
extracts values out of the 'delimiters' given in the
pattern. This (its in string form) is then converted
to the desired data type (integer).

The following header files would be needed:

#include <boost/regex.hpp>
#include <fstream>
#include <iostream>
#include <vector>

(plus: using namespace std; here)


(There are myriads of other ways to do that)

Regards

M.

James Kanze 06-25-2008 08:35 AM

Re: Reading an int array from file.
 
On Jun 25, 10:20 am, Mirco Wahab <wa...@chemie.uni-halle.de> wrote:
> Compass wrote:
> > I have an int array in a text file. The file structure is
> > like this:
> > [1, 2, 3, 4]
> > [5, 6, 7, 8]
> > [[1, 2], [3, 4], [5, 6], [7, 8]]


> > How can I easily read them in to three int arrays?


> The third line looks like an array of arrays.
> BTW, if it's only the numbers, you could simply
> read the file line-by-line and split the line on
> 'non-numbers'.


*IF* it's only the numbers he's interested in, and he doesn't
give two hoots about the other formatting, he could use a
filtering streambuf when parsing each line, which would convert
all non-digits to white space.

Somehow, however, I am very sceptical of this. As you say, the
last line above looks like it was meant to be an array of
arrays. More generally, it's usually preferable to verify
format (and anything else you can verify) when inputting text.
(boost::regex is a very powerful tool for this: if we exclude
the last line, then something like "\[\s*(\d+\s*,\s*)*\d+\s*\]"
should do the trick---not forgetting to double the \ if this is
a string constant. Once the line has been validated, of course,
using the filtering streambuf might be an appropriate solution.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


All times are GMT. The time now is 06:04 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.