parsing c code

Discussion in 'C' started by gar, Mar 29, 2006.

  1. gar

    gar New Member

    Joined:
    Mar 29, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    Firstly apologies for the long post. I just want to try to make clear what i'm doing. i'm trying to read in values from two files then perform an operation on them later to ouput to another file. The files i'm reading from have data like this look like this :

    Packet No.: 1
    system_time.wHour: 18
    system_time.wMinute: 31
    system_time.wSecond: 21
    system_time.wMilliseconds: 794
    Bytes Sent: 21

    The code i've developed so far deals with reading line by line, performing the required operations i.e. malloc and atoi. I'm not certain i'm attacking this the right way though the only error i get is with getline. Have tried to use fgets, fgetc and others. From reading through (many) posts i gather that i might read line using fgets and then parse it using sscanf. Is there a way i can use getline/fgetline? At the moment the error says that fgetline can't take 3 parameters. Should i make wholesale changes to code and try a different method? Any help would be appreciated, it's been driving me crazy.

    Here's a sample of some of the code:


    Code:
    :
    Code:
      
    int main()
    {
      int bytes_read;
      int nbytes = 100;
    
      char *my_string1;
      char *my_string2;
      char *my_string3; //etc
    ........
    .......
    FILE * fp1;
    int packetNo[1000];
     int time[1000];
    int counter = 0;
    
    
    fp1 = fopen("c:\\client.txt","r");
    
      my_string1 = (char *) malloc (nbytes + 1);
      my_string2 = (char *) malloc (nbytes + 1);
      my_string3 = (char *) malloc (nbytes + 1);
    
    while (1) {
      bytes_read = getline (&my_string1, &nbytes, fp1);
      bytes_read = getline (&my_string2, &nbytes, fp1);
      bytes_read = getline (&my_string3, &nbytes, fp1);
    
    packetNo[counter] = atoi(&my_string1[12]); 
    
      time[counter] = atoi(&my_string3[12]) * 1000 + atoi(&my_string4[20]);
     
       counter++;
    	
      }
    
    fclose (fp1);
    
    return 0;
    }
     
  2. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    I could not understand where the problem is. getline does take 3 parameters.

    istream& getline (char* s, streamsize n, char delim );
    Parameters.
    s » A pointer to an array of characters.
    n » The maximum number of characters to store, including the ternimating null character.
    delim » The delimiter. The operation of extracting succesive characters is stopped when delimiter is read. This parameter is optional, if not specified the function considers '\n' the delimiter.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice