Fgets and Fprintf Errors?

This is a discussion on Fgets and Fprintf Errors? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> typedef struct { char studentName[50]; int studentId; } database; int main(int ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    29

    Fgets and Fprintf Errors?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    typedef struct
    {
            char studentName[50];
            int studentId;
    }       database;
    
    int main(int argc, char *argv[])
    {
        database school;
        FILE *file_ptr;
        char text[50];
        int begin,temp;
        
        while(1)
        {
        printf("Input Data? (Yes = 1) : ");
        scanf("%d",&begin);
        
                  if(begin != 1)          
                  {
                  break;
                  }
        else
        {
                printf("\nEnter Student Name: ");
                fgets(school.studentName, 50 , stdin);
                
                printf("\nEnter Student ID: ");
                scanf("%d", &school.studentId);
                      
                printf("\nAdd to school.csv ?(y/n) : ");
                getchar();
                scanf("%c", &temp);
                    
                            if(temp == 'y' || temp == 'Y')
                            {
                                    file_ptr = fopen("school.csv" , "a");
                                    printf("\nFile school.csv opened\n");
                                    fprintf(file_ptr, "%s, %d,\n", school.studentName, school.studentId);
                                    fclose(file_ptr);
                                    printf("File Saved.\n\n");
                            }
                                   
                          
                    else
                    {
                    printf("\nFile not Saved.\n\n");   
                    }
    
    }   
    }
      system("PAUSE");	
      return 0;
    }
    The program is skipping over the student name input, so Im assuming the fget is incorrect, how do i fix that?
    Code:
                printf("\nEnter Student Name: ");
                fgets(school.studentName, 50 , stdin);
    Also, when I get to the prompt to save, I enter y and it says file not saved. Is it because my fprintf is wrong, if so how do i fix it?
    Code:
    printf("\nAdd to school.csv ?(y/n) : ");
                getchar();
                scanf("%c", &temp);
                    
                            if(temp == 'y' || temp == 'Y')
                            {
                                    file_ptr = fopen("school.csv" , "a");
                                    printf("\nFile school.csv opened\n");
                                    fprintf(file_ptr, "%s, %d,\n", school.studentName, school.studentId);
                                    fclose(file_ptr);
                                    printf("File Saved.\n\n");
                            }
    other than that, it complies with no bugs atm =/
    Last edited by Charak; 03-23-2011 at 08:17 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,693
    The program is skipping over the student name input, so Im assuming the fget is incorrect, how do i fix that?
    Your problem is probably the new line character in the input buffer from the previous scanf(). You can try to put a getchar() after the first scanf().

    Jim

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    Quote Originally Posted by jimblumberg View Post
    Your problem is probably the new line character in the input buffer from the previous scanf(). You can try to put a getchar() after the first scanf().

    Jim
    That worked thank you. Now I need to fix my fprintf so that'll save the file

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    typedef struct
    {
            char studentName[50];
            int studentId;
    }       database;
    
    int main(int argc, char *argv[])
    {
        database school;
        FILE *fp;
        char text[50];
        int begin,temp;
        
        while(1)
        {
        printf("Input Data? (Yes = 1) : ");
        scanf("%d",&begin);
        getchar();
        
                  if(begin != 1)          
                  {
                  break;
                  }
        else
        {
                printf("\nEnter Student Name: ");
                fgets(school.studentName, 50 , stdin);
                
                printf("\nEnter Student ID: ");
                scanf("%d", &school.studentId);
                      
                printf("\nAdd to school.csv ?(y/n) : ");
                scanf("%c", &temp);
                getchar();
                    
                            if(temp == 'y' || temp == 'Y')
                            {
                                    FILE *fp = fopen("school.csv" , "a");
                                    
                                    if (fp != NULL)
                                    {
                                    printf("\nFile school.csv opened\n");
                                    }
    
                                    fprintf(fp, "%s, %d,\n", school.studentName, school.studentId);
                                    fclose(fp);
                                    printf("File Saved.\n\n");
                            }
                                   
                          
                    else
                    {
                    printf("\nFile not Saved.\n\n");   
                    }
    
    }   
    }
      system("PAUSE");	
      return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,693
    The problem with your file writing is probably in this logic.
    Code:
                      if(temp == 'y' || temp == 'Y')
    First temp is defined as an int and you are using here as a character.

    Second, where do you ever assign a value to this variable?

    Jim

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    Quote Originally Posted by jimblumberg View Post
    The problem with your file writing is probably in this logic.
    Code:
                      if(temp == 'y' || temp == 'Y')
    First temp is defined as an int and you are using here as a character.

    Second, where do you ever assign a value to this variable?

    Jim

    I changed int temp to char temp;


    I dont understand the 2nd question. scanf("%c", &temp); ?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,693
    I dont understand the 2nd question. scanf("%c", &temp); ?
    Sorry I missed that in your code.

    Jim

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    it will print if I remove the else and change it to if(temp = 'y') but thats not what I want

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,693
    Code:
              scanf("%d", &school.studentId);
                      
                printf("\nAdd to school.csv ?(y/n) : ");
                scanf("%c", &temp);
    In the above code the first scanf is leaving the newline character in the buffer, you need to remove the newline character with getchar().

    Jim

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    int begin,temp;
    
    ...
    
    printf("\nEnter Student ID: ");
    scanf("%d", &school.studentId);
                      
    printf("\nAdd to school.csv ?(y/n) : ");
    scanf("%c", &temp);
    #1. temp is an int so you should either change the format specifier in the scanf call to %d or make temp a char.

    #2. If temp is made a char and you are using a %c format specifier, then this scanf call will need to have the input buffer cleared as you did before prior to calling it. This can be done as suggested by jimblumberg with another getchar call, or by altering the scanf call to have a space in front of the %c format specifier.
    Code:
    printf("\nAdd to school.csv ?(y/n) : ");
    scanf(" %c", &temp); /* Notice space in front of %c here! */
    The space lets scanf know that it should ignore leading whitespace before processing user input.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    thank you, its finally fixed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function
    By mattz in forum C Programming
    Replies: 1
    Last Post: 12-06-2001, 01:26 PM
  2. Array, reading in response etc...
    By mattz in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 10:41 AM
  3. Character in Array to end for loop.
    By mattz in forum C Programming
    Replies: 6
    Last Post: 12-04-2001, 10:05 AM
  4. Moving to the next structure array
    By mattz in forum C Programming
    Replies: 2
    Last Post: 11-30-2001, 02:43 PM
  5. Structure problem
    By mattz in forum C Programming
    Replies: 10
    Last Post: 11-30-2001, 12:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21