Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   parsing c code (http://www.go4expert.com/forums/parsing-c-code-t670/)

gar 29Mar2006 21:32

parsing c code
 
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: cpp

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;
}


coderzone 29Mar2006 22:53

Re: parsing c code
 
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.


All times are GMT +5.5. The time now is 05:04.