#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    2
    Rep Power
    0

    Exclamation parsing a text file


    hii all!
    am a begginer and have been banging my head over a simple problem.
    Using C++ I have to read from a text file which looks something like

    lines 30

    3.949 4.949 c
    2.343 4.454 c
    4.454 3.34334 c
    3.434 3.432 v
    2.343 3.53555 v


    the first line tells how many data points are there ...and then we have the x-coordinate and y-coordinate and a character c or v
    i have to read this data strip out the number of lines (i.e is the first line......lines 30) from the text file and store it in a structure and make array out of the structure.

    typedef struct{
    real x;
    real y;
    char t;
    } try;

    am stuck over this.......plzz help!!
    Last edited by siddz; September 18th, 2003 at 12:39 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2003
    Posts
    778
    Rep Power
    12
    HI,

    Don't know what your platform is, but most platforms do have a regular expression library.
    These are bit cumbersome to use with C++ though.

    Given the choice the best language for text processing still happens to be perl :-)
    A lean and mean secure FTP applet with GUI. Just 150 kb in size
    http://www.radinks.com/sftp
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Flip ahead in your book (I assume that you're using one or have one as a reference) to iostream. I'll give you a basic idea of what to look for, mainly because I work much more in C instead of C++ and because I never could see much use of streams -- but using them here will be the easier option for you.

    Declare a file stream (of type ifstream if I recall correctly) and call its open method. Then use the input operator, >>, to read in each value. After you've read the line for the number of records, then it should look something like this:
    Code:
    typedef struct
    { 
        double x; 
        double y; 
        char t; 
    } try; 
     
    try try_record;
    ifstream  ifile;
    
    // open ifile and read the first line to get the record count
    
    // for each record
    ifile >> try_record.x;
    ifile >> try_record.y;
    ifile >> try_record.t;
    // now process that record; e.g. store it in an array
    There are a lot of details that I'm leaving out plus I could be making some mistakes here (remember, I don't normally use this), so be sure to review the material in your book.

    The thing that makes this easier for you is that ifstream will treat the spaces and newline as delimiters separating the values being read, so you don't have to deal with them explicitly.

    Though I have to agree with e4c5 that the best language for text processing is perl. However, I've found some cases where I had a lot more control over the program with C. Everything's a trade-off.
    Last edited by dwise1_aol; September 18th, 2003 at 04:18 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    13
    Perl may be a great language for text processing, but it's power is overkill here. This is a trivial program in C or C++. I have every confidence that our student will be able to handle his homework without resorting to perl.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally posted by ClayDowling
    Perl may be a great language for text processing, but it's power is overkill here. This is a trivial program in C or C++. I have every confidence that our student will be able to handle his homework without resorting to perl.
    Ah, but wouldn't that be a great red herring?

    He should know now what he needs to do. It's still up to him to figure out how to do it and how to make it work.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    2
    Rep Power
    0
    Hey dwise1_aol, ClayDowling & e4c5 !!!

    Thanks a lot guys!!! was able to parse the file as required....:D

    u guys were of gr8 help...;)

IMN logo majestic logo threadwatch logo seochat tools logo