Thread: Text File not writing?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    6

    Text File not writing?

    Hi everyone I am trying to make student records system and write all the information that the user has typed into a text file. This is my attempt. It runs without any errors but when i go to check the text file nothing is written inside Please help !
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define MAX_STUDENT 20
    
    struct file {
        int year_of_stud,sem;
        char name[40],gender,programme,student_id[15];
        double fee;
    }Student[MAX_STUDENT];
    
    int main(void)
    {
        FILE *file;
        char ans='Y';
        int count = 0;
        int i=0;
        file = fopen ("Student.txt", "w");
    
        printf("How many students do you wish to add? ");
        scanf("%d", &count);
        getchar();
        //count = MAX_STUDENT;
        fflush(stdin);
    
    
    
    
    
    
        for(i=0;i<count;i++)
    {
    
        printf("Student #%d\n",i+1);
        printf("===========\n");
        printf("Enter Student ID : \n");
        scanf("%[^\n]", Student[i].student_id);
        fflush(stdin);
        printf("Please enter the student's name : \n");
        scanf("%[^\n]", Student[i].name);
        fflush(stdin);
        printf("Enter Gender : \n");
        scanf("%c", &Student[i].gender);
        fflush(stdin);
        printf("Enter Programme Enrolled In : \n");
        scanf("%s", &Student[i].programme);
        fflush(stdin);
        printf("Enter Year of Study : \n");
        scanf("%d", &Student[i].year_of_stud);
        printf("Enter Current Semester : \n");
        scanf("%d", &Student[i].sem);
        printf("Enter Fee's of Course : \n");
        scanf("%lf", &Student[i].fee);
        fflush(stdin);
        }
    
        if(i == 0)
                {
                    //count = i;
    
                    for(i=0; i<count; i++)
                    {
                        fprintf(file, "Student #%d\n", i+1);
                        fprintf(file, "-------------\n");
                        fprintf(file, "%s\n", Student[i].student_id);
                        fprintf(file, "%s\n", Student[i].name);
                        fprintf(file, "%c\n", Student[i].gender);
                        fprintf(file, "%s\n", Student[i].programme);
                        fprintf(file, "%d\n", Student[i].year_of_stud);
                        fprintf(file, "%d\n", Student[i].sem);
                        fprintf(file, "%.2lf\n\n", Student[i].fee);
                    }
                }
    
                fclose(file);
        
    
        return 0;
    
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You never check to see if fopen worked or not.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    Why do you have a "if( i == 0 )" line 57? "i" will probably never be equal to 0 at that place, unless "count" is equal to 1 or 0, since "i" will be equql to "count-1" at the end of the for loop. So you are never writing in your file.

    You should also check the inputed number for bad values, and the return of "fopen" as quzah said.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    Quote Originally Posted by quzah View Post
    You never check to see if fopen worked or not.


    Quzah.
    sorry but i have no idea how to check that.. still kinda new to the whole C programming thing

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ShiroiShu View Post
    Hi everyone I am trying to make student records system and write all the information that the user has typed into a text file. This is my attempt. It runs without any errors but when i go to check the text file nothing is written inside Please help !
    Referring to message #1 in the thread...
    Line 16 ... count = MAX_STUDENT;
    Delete lines 23, 24, 54, 57 ,58 and 73
    Line 18... you need to check the file pointer to be sure it actually opens and abort the program if it does not.
    Line 46 ... programme is only a single char in the struct, you are trying to stuff a string into it.

    Fix your indentation.
    Fix your struct layout... one variable per line is best.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    fopen returns NULL if the opening of the file failed. Learn to read the documentation.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    fflush(stdin);
    Read: Why fflush(stdin) is wrong.
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Not re-writing to a text file.
    By phantom in forum C++ Programming
    Replies: 4
    Last Post: 06-22-2010, 05:52 AM
  2. Replies: 8
    Last Post: 05-05-2010, 02:43 PM
  3. Writing to a text file
    By Inept Pig in forum C Programming
    Replies: 6
    Last Post: 11-06-2002, 04:48 PM
  4. Writing to a column in a text file
    By olland in forum C Programming
    Replies: 2
    Last Post: 01-21-2002, 06:40 AM
  5. When writing to a text file how do you...
    By JamMan in forum C++ Programming
    Replies: 4
    Last Post: 12-15-2001, 02:53 PM