1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Rep Power

    Fgets not reading last character

    I realized my mistake right after posting.
    My if condition shouldn't have been whether the length was less than the limit or not; it should have been whether the last character was a newline or not.
    The former condition works fine for all but the last line, but the last line doesn't have a newline after that, which is why it deleted my last character.

    Apologies to anyone who took the time to read this.

    The program basically echoes the data from an input file onto an output file.

    This is the input.
    1896 Summer
    US, UK, Russia
    1900 Summer
    Malaysia, Cambodia, Philippines
    1904 Summer
    Iran, Iraq, Kuwait
    1908 Summer
    Tunisia, Sudan, Guinea
    However, the output is this:
    event: 1896 Summer
    entrant: US, UK, Russia
    country: Greece
    place: Athens
    event: 1900 Summer
    entrant: Malaysia, Cambodia, Philippines
    country: France 
    place: Paris
    event: 1904 Summer
    entrant: Iran, Iraq, Kuwait
    country: US
    place: St.Louis
    event: 1908 Summer
    entrant: Tunisia, Sudan, Guinea
    country: UK
    place: Londo
    It's not reading the last 'n' of "London".

    I'll post the program.
    function fscan_oly scans individual data, and function get_ar calls fscan_oly for each array element.

    #include <stdio.h>
    #include <strings.h>
    #define LEN 40
    #define GNUM 10
    typedef struct
        char event[LEN],
    }  Olympic;
    char *fscan_oly(FILE *inp, Olympic *x);
    void fprint_oly(FILE *oup, Olympic x);
    int get_ar(FILE *inp, Olympic *x);
    int main()
        FILE *inp = fopen("input.txt", "r"), *oup = fopen("output.txt", "w");
        Olympic comp[GNUM];
        int i, siz;
        siz = get_ar(inp, comp);
        for(i = 0; i < siz; ++i)
            fprint_oly(oup, comp[i]);
    int get_ar(FILE *inp, Olympic *x)
        int i;
        Olympic data;
        char *status;
        status = fscan_oly(inp, &data);
        for(i = 0; i < GNUM && status != NULL; ++i)
            x[i] = data;
            status = fscan_oly(inp, &data);
            printf("status == NULL %d\n", status == NULL);
        return i;
    char *fscan_oly(FILE *inp, Olympic *x)
        char *ptr;
        fgets(x->event, LEN, inp);
        if(strlen(x->event) < LEN)
            x->event[strlen(x->event) - 1] = '\0';
        fgets(x->entrant, LEN, inp);
        if(strlen(x->entrant) < LEN)
            x->entrant[strlen(x->entrant) - 1] = '\0';
        fgets(x->country, LEN, inp);
        if(strlen(x->country) < LEN)
            x->country[strlen(x->country) - 1] = '\0';
        ptr = fgets(x->place, LEN, inp);
        if(strlen(x->place) < LEN)
            x->place[strlen(x->place) - 1] = '\0';
        return ptr;
    void fprint_oly(FILE *oup, Olympic x)
        fprintf(oup, "event: %s\n", x.event);
        fprintf(oup, "entrant: %s\n", x.entrant);
        fprintf(oup, "country: %s\n", x.country);
        fprintf(oup, "place: %s\n", x.place);
        fprintf(oup, "----------------------------\n");
    Last edited by 046; July 15th, 2013 at 11:46 PM. Reason: realized mistake right after posting
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Rep Power
    1. Check that the input file really does have a newline at the end.

    > x->place[strlen(x->place) - 1] = '\0';
    2. Don't blindly assume that the buffer has a newline. Check the last char is \n before removing it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo