468,496 Members | 1,819 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

how to use fgets

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
Sep 19 '06 #1
3 8833
It is not possible to get the fields in a line using fgets(). Because no field is of fixed length.
Sep 19 '06 #2
I believe what you are asking for is:

you have to read from that text file using delimeter "," to parse (separate the data going into an array, and then search that array for whatever values you need...
Sep 19 '06 #3
risby
30
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]
Sep 19 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Tanel | last post: by
5 posts views Thread by Rob Somers | last post: by
35 posts views Thread by David Mathog | last post: by
11 posts views Thread by santosh | last post: by
32 posts views Thread by FireHead | last post: by
9 posts views Thread by uidzer0 | last post: by
285 posts views Thread by Sheth Raxit | last post: by
26 posts views Thread by Bill Cunningham | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.