Reading an int array from file.

Discussion in 'C++' started by Compass, Jun 25, 2008.

  1. Compass

    Compass Guest

    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!
     
    Compass, Jun 25, 2008
    #1
    1. Advertising

  2. Compass

    Mirco Wahab Guest

    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.
     
    Mirco Wahab, Jun 25, 2008
    #2
    1. Advertising

  3. Compass

    James Kanze Guest

    On Jun 25, 10:20 am, Mirco Wahab <-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:
    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
     
    James Kanze, Jun 25, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. S300
    Replies:
    4
    Views:
    22,003
    Roedy Green
    Aug 19, 2003
  2. Schnoffos
    Replies:
    2
    Views:
    1,222
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,649
    Old Wolf
    Jan 20, 2004
  4. arun
    Replies:
    8
    Views:
    460
    Dave Thompson
    Jul 31, 2006
  5. Alain Spineux
    Replies:
    6
    Views:
    634
    Eric Sosman
    May 17, 2011
Loading...

Share This Page