C++ Class to do File Text Based Operations

Discussion in 'C++' started by anand, May 28, 2005.

  1. anand

    anand Guest

    Hi,

    I am looking for a possible C++ File class that allows to do standard
    "Perl Like" operations on structured data files in ASCII text.

    For example:

    1) Get the first and third fields from each line into a string obj
    2) Get ALL "words" in a "line" of the file

    etc...

    Basically, anything you would be able to do from a Perl script, but
    thru C++. I am looking to use it in my program and I know there are
    better programmers than me out there, so I thought I will check first!

    THanks in advance!
    Anand


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    anand, May 28, 2005
    #1
    1. Advertising

  2. "anand" <> wrote in message
    news:...
    > I am looking for a possible C++ File class that allows to do standard
    > "Perl Like" operations on structured data files in ASCII text.
    >
    > For example:
    >
    > 1) Get the first and third fields from each line into a string obj
    > 2) Get ALL "words" in a "line" of the file
    >
    > etc...
    >
    > Basically, anything you would be able to do from a Perl script, but
    > thru C++. I am looking to use it in my program and I know there are
    > better programmers than me out there, so I thought I will check first!


    The two operations above can be written with a standard C++ istream.
    But you may want to consider using a regular expressions library
    for C++. Such as boost::regex http://www.boost.org/libs/regex/doc/.


    Ivan

    NB: it is often redundant to post to both clc++ and clc++.moderated.
    If you're concerned by off-topic replies/threads, use the moderated group.
    If you want a quicker answer, clc++ is usually best.
    But posting to both NGs usually will delay the appearance of your
    post and reply on clc++ as well.

    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
    Ivan Vecerina, May 28, 2005
    #2
    1. Advertising

  3. anand wrote:

    > Basically, anything you would be able to do from a Perl script, but
    > thru C++.


    That will be very difficult. You need to implement a C++ version of all
    modules in CPAN, at least.

    If you want to use all Perl machinery, the easiest way will be to embed perl
    in your program.

    --
    Salu2


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    =?ISO-8859-15?Q?Juli=E1n?= Albo, May 28, 2005
    #3
  4. anand

    Jeff Flinn Guest

    "anand" <> wrote in message
    news:...
    > Hi,
    >
    > I am looking for a possible C++ File class that allows to do standard
    > "Perl Like" operations on structured data files in ASCII text.
    >
    > For example:
    >
    > 1) Get the first and third fields from each line into a string obj
    > 2) Get ALL "words" in a "line" of the file
    >
    > etc...
    >
    > Basically, anything you would be able to do from a Perl script, but
    > thru C++. I am looking to use it in my program and I know there are
    > better programmers than me out there, so I thought I will check first!


    Check out:

    http://www.boost.org/libs/regex/doc/index.html

    http://www.boost.org/libs/spirit/index.html

    http://www.boost.org/doc/html/string_algo.html

    http://www.boost.org/libs/filesystem/doc/index.htm

    I'm totally unfamiliar with Perl, but the above libraries offer powerful C++
    text and filesystem processing.

    Jeff Flinn



    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Jeff Flinn, May 30, 2005
    #4
  5. anand

    Billy Patton Guest

    On Sat, 28 May 2005, anand wrote:

    > Hi,
    >
    > I am looking for a possible C++ File class that allows to do standard
    > "Perl Like" operations on structured data files in ASCII text.
    >
    > For example:
    >
    > 1) Get the first and third fields from each line into a string obj
    > 2) Get ALL "words" in a "line" of the file
    >
    > etc...
    >
    > Basically, anything you would be able to do from a Perl script, but
    > thru C++. I am looking to use it in my program and I know there are
    > better programmers than me out there, so I thought I will check first!
    >
    > THanks in advance!
    > Anand


    Couple of functions I wrote do do just what you ask. Excep no regular
    expressions. THere is a split and a sub. You could extend these to use pcre
    lib and get the location of a regular expression. I just haven't carried it
    that far YET :)

    #include <string>
    #include <sstream>
    #include <locale>

    bool StrSplit(const string in,char delim,vector<string>& out)
    {
    if (in.empty()) return false;
    istringstream iss(in);
    string token;
    out.clear();
    while(getline(iss, token, delim))
    out.push_back(token);
    return true;
    }

    bool StrSub(string& cp,string sub_this,string for_this,int num_times)
    {
    int i,loc;
    if (cp.empty())
    {
    cp = sub_this;
    return true;
    }
    if (for_this.empty()) return false;
    for (i = 0; i NE num_times; i++)
    {
    loc = cp.find(for_this,0);
    if (loc GE 0) cp.replace(loc,for_this.length(),sub_this);
    else return true;
    }
    return true;
    }

    ___ _ ____ ___ __ __
    / _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
    / _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
    /____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
    /___/
    Texas Instruments ASIC Circuit Design Methodology Group
    Dallas, Texas, 214-480-4455,

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Billy Patton, Jun 1, 2005
    #5
    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. BLF
    Replies:
    4
    Views:
    1,268
    Mike Treseler
    Aug 7, 2004
  2. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,043
    robert
    Feb 11, 2006
  3. Daniel Gutson
    Replies:
    10
    Views:
    550
    Pete Becker
    Jan 11, 2008
  4. Daniel Gutson
    Replies:
    3
    Views:
    522
    James Kanze
    Jan 10, 2008
  5. James Kanze
    Replies:
    3
    Views:
    373
    Daniel Gutson
    Jan 11, 2008
Loading...

Share This Page