Thread: Reading from Files, and printing to them

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    9

    Reading from Files, and printing to them

    Hello guys,

    I am having difficulty maintaining a file of student information, using C, and any advice/hints would be greatly appreciated .

    Problem background --> First off, I need to create a program that will read student names, ids, grades, etc. from a .txt file into a structure variable. Then the user should have a choice to add a student to the file, or look up a student using the student id number.

    I am having problems in a couple of places:
    1. Scanning the file line by line until I reach the end
    2. Searching through the structure to match student ID
    Here is my partial code:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int main (void)
    
    
    {
        FILE *fptr;
        char Mystring[100];
        int studentID;
        char lastname[50];
        char username[50];
        char userltname[50];
        int ctr, secondctr, thirdctr;
        int userinput;
    
    
        //Below is my structure to store info from .txt file
        struct studentrecord
            {
                char firstname[50];
                char lastname[50];
                int student_number;
                int ast1, ast2, ast3, ast4, ast5 ;
                int lab1, lab2, lab3, lab4, lab5;
                int midterm;
                int final;
    
    
    
    
            };
    
    
        struct studentrecord GNG1106[200];
    
    
        fptr=fopen("test.txt", "r+");
        if (fptr==NULL)
            printf("Unable to open file.");
    
    
        else
            {
                printf("FILE OPENED");
                ctr=0;                  /*I'm scannig using for loop, but in the future I won't know the number of additions so I need a better way*/
                while (ctr<6)
                {
                    //Scan the file and store in structure
                    fscanf(fptr, "%d", &GNG1106[ctr].student_number);
                    fscanf(fptr,"\n%s", GNG1106[ctr].firstname);
                    fscanf(fptr,"%s", GNG1106[ctr].lastname);
                    fscanf(fptr, "%d", &GNG1106[ctr].ast1);
                    fscanf(fptr, "%d", &GNG1106[ctr].ast2);
                    fscanf(fptr, "%d", &GNG1106[ctr].ast3);
                    fscanf(fptr, "%d", &GNG1106[ctr].ast4);
                    fscanf(fptr, "%d", &GNG1106[ctr].ast5);
                    fscanf(fptr, "%d", &GNG1106[ctr].lab1);
                    fscanf(fptr, "%d", &GNG1106[ctr].lab2);
                    fscanf(fptr, "%d", &GNG1106[ctr].lab3);
                    fscanf(fptr, "%d", &GNG1106[ctr].lab4);
                    fscanf(fptr, "%d", &GNG1106[ctr].lab5);
    
    
                    fscanf(fptr,"%d", &GNG1106[ctr].midterm);
                    fscanf(fptr,"%d", &GNG1106[ctr].final);
                    printf("\n%s %s %d %d %d %d %d %d %d %d %d %d %d %d %d", GNG1106[ctr].firstname, GNG1106[ctr].lastname, GNG1106[ctr].student_number, GNG1106[ctr].ast1, GNG1106[ctr].ast2, GNG1106[ctr].ast3, GNG1106[ctr].ast4, GNG1106[ctr].ast5, GNG1106[ctr].lab1, GNG1106[ctr].lab2, GNG1106[ctr].lab3, GNG1106[ctr].lab4, GNG1106[ctr].lab5, GNG1106[ctr].midterm, GNG1106[ctr].final);
    
    
                ctr++;
    
    
                }
    
    
            printf("\n\n\nPlease enter a valid student number=>");
            scanf("%d", &userinput);
    
    
            do      /*Here I get an infinite loop */
            {
                ctr=0;
                if (userinput==GNG1106[ctr].student_number)
                printf("\n%s %s %d %d %d %d %d %d %d %d %d %d %d %d %d", GNG1106[ctr].firstname, GNG1106[ctr].lastname, GNG1106[ctr].student_number, GNG1106[ctr].ast1, GNG1106[ctr].ast2, GNG1106[ctr].ast3, GNG1106[ctr].ast4, GNG1106[ctr].ast5, GNG1106[ctr].lab1, GNG1106[ctr].lab2, GNG1106[ctr].lab3, GNG1106[ctr].lab4, GNG1106[ctr].lab5, GNG1106[ctr].midterm, GNG1106[ctr].final);
                ctr++;
            }while (userinput!=GNG1106[ctr].student_number);
    
    
            fclose(fptr);
    
    
            }
    
    
    }
    I believe I've met my match with this program, and fear I may be slain by it.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The scanf() family of functions all return the number of objects that they have scanned and stored in a variable.

    So right after:
    int OK= fscanf() for the student number, if OK=0, then you have reached the end of the file, or a file read error has occurred. Set ctr to zero, and continue (that is, use the C keyword continue, when OK is 0). That will cause the loop to immediately be retested at the top, and it will return false, ending the looping.

    In your endless do while loop, you need to move the ctr=0 OUTSIDE the do while loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Reading and printing a file
    By T1m in forum C Programming
    Replies: 1
    Last Post: 01-08-2009, 01:29 PM
  3. List Reading - Printing
    By ch4 in forum C Programming
    Replies: 3
    Last Post: 06-13-2007, 10:50 AM
  4. reading and printing
    By Rakansen in forum C Programming
    Replies: 11
    Last Post: 10-26-2006, 02:46 PM
  5. printing txt files
    By baphomet in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-10-2002, 09:53 PM

Tags for this Thread