Thread: program that extracts certain words from the string

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    24

    program that extracts certain words from the string

    hey guys,

    Can you please help me write a program that would divide lines into the fields. Each field is separated by comma. I only need to save nonnumerical fields in the array for each nonnumerical column. For example:

    0,tcp,http,SF,181,5450,0,0,0,0,0,1,0,0,0,0,0,0,0,0 ,0,0,8,8,0.00,0.00,0.00,0.00,1.00,0.00,0.00,9,9,1. 00,0.00,0.11,0.00,0.00,0.00
    ,0.00,0.00,normal.
    0,udp,ftp,SF,239,486,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0 ,0,8,8,0.00,0.00,0.00,0.00,1.00,0.00,0.00,19,19,1. 00,0.00,0.05,0.00,0.00,0.0
    0,0.00,0.00,normal.
    0,tcp,smtp,SF,235,1337,0,0,0,0,0,1,0,0,0,0,0,0,0,0 ,0,0,8,8,0.00,0.00,0.00,0.00,1.00,0.00,0.00,29,29, 1.00,0.00,0.03,0.00,0.00,0.
    00,0.00,0.00,normal.
    0,tcp,http,SF,219,1337,0,0,0,0,0,1,0,0,0,0,0,0,0,0 ,0,0,6,6,0.00,0.00,0.00,0.00,1.00,0.00,0.00,39,39, 1.00,0.00,0.03,0.00,0.00,0.
    00,0.00,0.00,smurf.

    I would like to save each nonnumerical column (in this case 2nd column - starting with tcp, 3rd column starting with http, 4th column - SF and 42nd column - starting with normal) in separate array. Each array would contain different values for the same field in the column. (For example, 3rd column contains http, ftp, smtp, etc...)
    How would you do this with a C++ program (not standard C)?

    Thanks a lot!

    ~Dmitry
    Dmitry Kashlev

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784

    Re: program that extracts certain words from the string

    Originally posted by MKashlev

    0,tcp,http,SF,181,5450,0,0,0,0,0,1,0,0,0,0,0,0,0,0 ,0,0,8,8,0.00,0.00,0.00,0.00,1.00,0.00,0.00,9,9,1. 00,0.00,0.11,0.00,0.00,0.00
    ,0.00,0.00,normal.
    0,udp,ftp,SF,239,486,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0 ,0,8,8,0.00,0.00,0.00,0.00,1.00,0.00,0.00,19,19,1. 00,0.00,0.05,0.00,0.00,0.0
    0,0.00,0.00,normal.

    How would you do this with a C++ program (not standard C)?

    ~Dmitry
    The file is in error. Look:
    0,tcp,http,...
    0,udp,ftp,...

    The size is not uniform. This is a problem and to fix it your file should look like this:
    0,tcp,http,...
    0,udp,fpt ,...

    In other words, you need to make each filed as large as the largest datamember. This means leaving white spaces, that way it is very easy to use a structure to efficiently parse everything. You really only learn this in a language like Cobol.

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    vector<struct rec> History;

    while (fgets(cArray,256,fptr) != NULL)
    {

    sscanf(cArray,"%2*,%3c,%1*,%4c", rec.item1, rec.item2);

    History.push_back (rec);
    }
    0,tcp,http,...
    12 1
    Last edited by Troll_King; 08-06-2002 at 10:07 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with string program
    By Duskan in forum C Programming
    Replies: 8
    Last Post: 04-02-2007, 08:27 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM