if i have the following data in a text file;
BHp,"12 feb 2006",32.42,2424.35,3535.535
BHP,"13 feb 2006",434.35,3535.5454,353
and so on
using fgets how can i get the program to read the column of BHP, date
each by itself
You read in a line at a time with fgets() and then you can use strtok() to extract each field in which you are interested.
[php]
while (!feof(i_file)){
while (NULL != fgets(string, BUFSIZ, i_file)){
field_counter = 1;
field = strtok(string, field_delimiters);
while (NULL != field){
switch (field_counter){
case 1:
/* do something with 1st field */
fprintf(o_file, "%s", field);
break;
case 2:
/* do something with 2nd field */
fprintf(o_file, ", %s\n", field);
break;
/* and so on ...
case n:
break;
*/
}
field = strtok(NULL, field_delimiters);
field_counter++;
}
}
if (ferror(i_file)){
perror("Error reading input file");
exit(1);
break;
}
}
[/php]