Thread: Parsing a file using C program

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    1

    Parsing a file using C program

    Gurus,

    I have a file with the below content:

    ALT_ATF2_B0_MAP [UINT(16),(1,5)] 5557 ; 5557
    : 7075 ; 7075
    : 7075 ; 7075
    : 7075 ; 7075
    : 7075 ; 7075
    ALT_ATF2_B1_MAP [UINT(16),(1,5)] 10250 ; 10250
    : 12621 ; 12621
    : 12621 ; 12621
    : 12621 ; 12621
    : 12621 ; 12621
    ALT_ATF2_B2_MAP [UINT(16),(1,5)] 4776 ; 4776
    : 5692 ; 5692
    : 5692 ; 5692
    : 5692 ; 5692
    : 5692 ; 5692
    ALT_ATF2_VEHICLE_SPEED_MAP_VTD [UINT(16),(1,5)] 0 ; 0
    : 640 ; 640
    : 1280 ; 1280
    : 2944 ; 2944
    : 5760 ; 5760
    ALT_COMP_EAC_A1 [INT(16)] 12205 ; 12205
    ALT_COMP_EAC_A2 [INT(16)] 4072 ; 4072
    ALT_COMP_EAC_B0 [INT(16)] 7674 ; 7674
    ALT_COMP_EAC_B1 [INT(16)] 14221 ; 14221
    ALT_COMP_EAC_B2 [INT(16)] 6606 ; 6606
    ALT_COMP_EAC_BOOST [INT(16)] 164 ; 20.5

    In the above file, I need to parse and read only those lines which have ",(" as delimiter for e.x
    ALT_ATF2_VEHICLE_SPEED_MAP_VTD [UINT(16),(1,5)] 0 ; 0

    Once we encounter this string, I need to do the following:
    1) Store the first string in that line to a variable e.x ALT_ATF2_VEHICLE_SPEEAD_MAP_VTD needs to be stored in a variable
    2) Secondly, at the end of the line there are 0; 0 and there are 4 more lines below this line. All are part of one entry. For E.x
    ALT_ATF2_VEHICLE_SPEED_MAP_VTD [UINT(16),(1,5)] 0 ; 0
    : 640 ; 640
    : 1280 ; 1280
    : 2944 ; 2944
    : 5760 ; 5760

    In the above, after we store ALT_ATF2_VEHICLE_SPEED_MAP_VTD in one variable. This corresponds to a property name in our software and has to be assigned with an array of values, which are nothing but 0, 640, 1280, 2944, 5760. These have to be the first values in the set ( for e.x 640; 640 => we need to store the first 640 only in the array).

    We need to get all the first values into an array till we see another totally independent line entry like ALT_COMP_EAC_A1 [INT(16)] 12205 ; 12205

    Once the values are obtained, we can use our Software API's to set the property with the obtained set of array of values.

    Appreciate if anyone can help me with the parsing logic as I am finding it difficult to code.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Why are you doing this in C? If there was ever a job for Perl, you've found it.

    Ignoring that what have you written so far? I'd imagine you'd make use of the strstr() function for starters to look for ",(".

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Read a line.
    Search for ,( (if that's your key phrase)
    If you find it, then split apart the line you're on into its pieces (sscanf seems like a plan here)
    If you don't, then see if it starts with a : -- if so, read some numbers
    If it doesn't, then presumably you've got one of those one-line things that you don't seem to care about.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Read the homework policy. Then, read the docs for strstr, fscanf and maybe strtok. Show us some effort by posting the code you've tried, and ask some more specific questions, and you'll probably get much better help.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    27
    I've found this code on the net:
    Code:
    void Tokenize(const string str, vector<string>& tokens, const string& delimiters){ 
       int startpos = 0;
        int pos = str.find_first_of(delimiters, startpos);    
    string strTemp;    
    while (string::npos != pos || string::npos != startpos)    {        
    strTemp = str.substr(startpos, pos - startpos);        
    tokens.push_back(strTemp.substr(0, strTemp.length()));        
    startpos = str.find_first_not_of(delimiters, pos);        
    pos = str.find_first_of(delimiters, startpos);    }}
    Of course, it is written in C++, so you have to find some vector routine for C.
    Last edited by derder; 08-01-2011 at 12:45 PM.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    35
    Quote Originally Posted by KCfromNC View Post
    Why are you doing this in C? If there was ever a job for Perl, you've found it.

    Ignoring that what have you written so far? I'd imagine you'd make use of the strstr() function for starters to look for ",(".
    Wow. I was gonna learn Perl but I put it aside coz I thought nothing can beat C for parsing. Is Perl really better in this regard?

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by derder View Post
    I've found this code on the net:
    Please don't go searching the net the posting *other people's code* here. This is a C programming forum, it's about learning and writing C code... it is NOT about trading code snippets found on the web.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Perl is a general purpose language, but has extensive parsing text for report writing, features.

    You can parse the stink out of ........ with C if you like, but it's not as easy. If you already know C, I would use it, but if you're going to be learning a new language for this purpose, then Perl is the better language for the job.

    Having said that, you seem to have the logic already well thought out, so if you have included string.h, then:

    fgets() each line into a one line char buffer

    use strstr() on that buffer to see if it has the target ,( chars.

    Save your data.

    Parsing strings is an acquired taste.
    Last edited by Adak; 08-01-2011 at 02:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with file parsing
    By sup_stephen in forum C Programming
    Replies: 13
    Last Post: 04-13-2008, 05:23 PM
  2. parsing a file
    By avgprogamerjoe in forum C++ Programming
    Replies: 2
    Last Post: 12-22-2007, 06:09 PM
  3. xml file parsing in C
    By lonbgeach in forum Tech Board
    Replies: 31
    Last Post: 12-14-2006, 02:14 AM
  4. String parsing(parsing comments out of HTML file)
    By slcjoey in forum C# Programming
    Replies: 0
    Last Post: 07-29-2006, 08:28 PM
  5. XML File Parsing
    By WebSnozz in forum C++ Programming
    Replies: 0
    Last Post: 04-21-2002, 03:24 AM