By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 911 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

how to use fgets

P: 25
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
Share this Question
Share on Google+
3 Replies


P: 61
It is not possible to get the fields in a line using fgets(). Because no field is of fixed length.
Sep 19 '06 #2

P: 10
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
P: 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.