Re: data file reading and manipulate

From: Karl Heinz Buchegger (kbuchegg_at_gascad.at)
Date: 06/04/04

  • Next message: Karl Heinz Buchegger: "Re: Exercise problem"
    Date: Fri, 04 Jun 2004 14:54:34 +0200
    
    

    Edo wrote:
    >
    > the str1 is a word not a "one letter" I which there is regex it could
    > have been better to handle with it.
    > isn't there some library to read comma seperated files and put them into
    > some containers of choice... humm

    getline() will do what you want. YOu cen tell it what you consider
    to be the end of the line. The default is '\n', but you can use
    any character you want, eg. ',' Thus you can read the text parts
    of the line by using getline and the numeric part with <<

    Another approach:

    google is your friend. You are not the first one to have that
    specific problem. In
    http://www.google.com section: groups
    there is a huge archive of all the common news groups.
    A search phrase of: "reading comma delimited stream C++"
    turns up with lots of postings. Among them (actually it
    is the first) is one from Jerry Coffin

    ---------------------------------------------------------------------------

    As long as you're certain that commas are _only_ used to separate
    fields, NOT embedded inside of a field (e.g. surrounded by quotes)
    there's a way that works nicely and doesn't take a lot of code, but
    initially seems very strange (or at least it did to me).

    A stream uses the locale to tell what characters are what -- e.g.
    what's a letter, what's a digit, and (importantly) what's white
    space. As far as a stream cares, "whitespace" is really a synonym
    for "separator".

    Therefore, to get the stream to easily read items separated by
    commas, we have the stream use a locale that tells it commas are
    whitespace:

    #include <iostream>
    #include <locale>
    #include <algorithm>
    #include <limits>
    #include <vector>

    class my_ctype : public std::ctype<char>
    {
    // some compilers (e.g. vc++7) complain that table_size isn't
    // constant (even though it's defined const) in which case
    // UCHAR_MAX+1 will usually do the job.
     mask my_table[table_size];
    public:
        my_ctype(size_t refs = 0)
            : std::ctype<char>(my_table, false, refs)
        {
     // copy the original table.
            std::copy(classic_table(),
                      classic_table() + table_size,
                      my_table);
     
     // modify it so ',' is a space character.
            my_table[','] = (mask)space;
        }
    };

    int main() {

    // create an instance of our locale, and tell the stream to use it.
        std::locale x(std::locale::classic(), new my_ctype);
        std::cin.imbue(x);
        int temp;

        std::vector<int> vec;

    // read (comma delimited) ints from the stream until EOF.
        while (std::cin>>temp)
            vec.push_back(temp);

    // write the ints out, one per line.
        std::ostream_iterator<int> out(std::cout, "\n");
        std::copy(vec.begin(), vec.end(), out);
        return 0;
    }

    -- 
        Later,
        Jerry.
    -- 
    Karl Heinz Buchegger, GASCAD GmbH
    Teichstrasse 2
    A-4595 Waldneukirchen
    Tel ++43/7258/7545-0 Fax ++43/7258/7545-99
    email: kbuchegg@gascad.at  Web: www.gascad.com
    Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
    

  • Next message: Karl Heinz Buchegger: "Re: Exercise problem"

    Relevant Pages