how to use fgets

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • georges the man
    New Member
    • Sep 2006
    • 25

    how to use fgets

    if i have the following data in a text file;

    BHp,"12 feb 2006",32.42,242 4.35,3535.535
    BHP,"13 feb 2006",434.35,35 35.5454,353
    and so on
    using fgets how can i get the program to read the column of BHP, date
    each by itself
  • pukur123
    New Member
    • Sep 2006
    • 61

    #2
    It is not possible to get the fields in a line using fgets(). Because no field is of fixed length.

    Comment

    • AlekseyUS
      New Member
      • Sep 2006
      • 10

      #3
      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...

      Comment

      • risby
        New Member
        • Sep 2006
        • 30

        #4
        Originally posted by georges the man
        if i have the following data in a text file;

        BHp,"12 feb 2006",32.42,242 4.35,3535.535
        BHP,"13 feb 2006",434.35,35 35.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_delimiter s);
        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_delimiter s);
        field_counter++ ;
        }
        }
        if (ferror(i_file) ){
        perror("Error reading input file");
        exit(1);
        break;
        }
        }
        [/php]

        Comment

        Working...