Thread: fgets & Structures

  1. #1
    Registered User JM1082's Avatar
    Join Date
    Mar 2011

    Thumbs down fgets & Structures

    Hi all,
    I'm trying to use fgets in a program so that my structure members can store spaces however when the program is run it prints my printf output then automatically inputs a blank string and skips to the following printf output.
    In example:
    int appointment(void)
        #define DATE_LEN 4
        #define START_LEN 2
        #define FINISH_LEN 2
        #define TITLE_LEN 20
        #define PLACE_LEN 30
        #define MEET_LEN 30
        #define FILE_NAME "E:\\appointment.txt"
        FILE *fp1;
        fp1 = fopen(FILE_NAME, "a");
        if (fp1 == NULL) {
            printf("Can't open %s\n", FILE_NAME);
        struct appointment {
            char date[DATE_LEN + 1];
            char start_time[START_LEN + 1];
            char finish_time[FINISH_LEN + 1];
            char title[TITLE_LEN + 1];
            char place[PLACE_LEN + 1];
            char meet[MEET_LEN + 1];
        } appointment;
        printf("\nAppointment date (ddmm): ");
        fgets(, sizeof(, stdin);
        printf("\n00 = 09:00, 01 = 09:30, 16 = 16:30\nAppointment start: ");
        fgets(appointment.start_time, sizeof(appointment.start_time), stdin);
        printf("\n1 = 09:30, 02 = 10:00, 17 = 17:00\nAppointment end: ");
        fgets(appointment.finish_time, sizeof(appointment.finish_time), stdin);
        printf("\nAppointment name: ");
        fgets(appointment.title, sizeof(appointment.title), stdin);
        printf("\nAppointment venue: ");
        fgets(, sizeof(, stdin);
        printf("\nAppointment with: ");
        fgets(, sizeof(, stdin);
        fprintf(fp1, "%s\n%s\n%s\n%s\n\n",, appointment.start_time, appointment.finish_time, appointment.title,,;
    So what's going on?
    Last edited by JM1082; 03-24-2011 at 12:07 PM. Reason: A few small changes, unrelated to original problem! ;-)

  2. #2
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    Do you use another method besides fgets to get user input in another part of your program? There's a good change you have a newline still stuck in the input buffer when you call appointment(). See if this helps: FAQ > Flush the input buffer.

  3. #3
    Join Date
    Dec 2007
    Don't forget...fgets() includes the terminating newline in the buffer you're reading into. You probably should read into a buffer you reuse and copy the data without the newline into the target variable.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Toronto, Canada
    You realize that your last fprintf has 6 arguments but only 4 formatter thingies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. two threads, two fgets(), one problem!
    By tmcp in forum C Programming
    Replies: 0
    Last Post: 02-22-2011, 10:26 PM
  2. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  3. Structures and dynamically allocated arrays
    By Bandizzle in forum C Programming
    Replies: 7
    Last Post: 10-04-2009, 02:05 PM
  4. fgets() and structures issue
    By gettyUP in forum C Programming
    Replies: 3
    Last Post: 12-21-2006, 11:26 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM

Tags for this Thread