Thread: Structure and file

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    40

    Structure and file

    Code:
    void getinfoStudent()
    {
    
    
        system("cls");
        int studentnum, i, j = 0;
        printf("Please type the student number to look at the student's information: ");
        scanf("%d",&studentnum);
    
    
    FILE *fp;
    
    
    fp = fopen("studentinfo.txt","r");
     if (fp == NULL) {
             printf("I couldn't open results.dat for printing.\n");
             exit(0);
          }
    
    
        for (i = 0; i<MAX; i++)
        {
            if(studentnum == student[i].studentnumber)
            {
                printf("First Name  Last Name  A1  A2  A3  A4  A5  L1  L2  L3  L4  L5  Midterm  Final\n");
                printf("%s %s %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
                       student[i].firstname,student[i].lastname,student[i].studentnumber,student[i].markAssignment[0],
                       student[i].markAssignment[1],student[i].markAssignment[2],student[i].markAssignment[3],student[i].markAssignment[4],
                       student[i].markLab[0],student[i].markLab[1],student[i].markLab[2],student[i].markLab[3],student[i].markLab[4],
                       student[i].markMidterm, student[i].markFinal);
                       fclose(fp);
                       break;
                       j = 1;
    
    
            }
    
    
        }
        if (j == 0)
        {
                  printf("Invalid student number. Please type it again.\n");
                getch();
                system("cls");
                getinfoStudent();
        }
    }
    I've posted wrong code in my previous thread, so I decided to make another thread.

    I'm trying to compare the scanned student number with the student number in the file. If the scanned student number matches to the student number in the file, the program prints the specific information of the student with that number. Otherwise, it prints invalid and goes back to scanning part.

    I've tried to use for loop to find if there is any numbers which match up to the number in the file. However, seems like it's not working properly.

    Could anyone help me with this problem?
    Thank you!!

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I dont really see anything from the code, which demonstrates that your reading information from the file. What youre doing there is effectively openning the file to read (assuming the right file). And after that you dont do anything with the file. I would imagine you will have to read data (each student record) from the file into a data strucuture of some kind.

    Have you looked in to function like fscanf, fgets, fread etc? What format is your student infomation file is in? Reading information from the file may involve some furthre processing, depending on the format the data has been preserved in the file.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    40
    my file is txt file

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    >my file is txt file
    sure, but what format the data is in the file. Can you post a sample from your text file. Jyst copy and paste from your file here?

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    In order to compare text entered by the user from stdin to that from a file, you need to read in the characters one way or another from the file.

    C File I/O Tutorial - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FILE within a structure
    By bnyec in forum C Programming
    Replies: 7
    Last Post: 04-25-2011, 03:25 PM
  2. File -> structure
    By Etdim in forum C Programming
    Replies: 5
    Last Post: 12-20-2007, 04:13 PM
  3. Replies: 9
    Last Post: 05-21-2007, 12:10 AM
  4. from a text file to a structure ?
    By Intenz in forum C Programming
    Replies: 3
    Last Post: 12-30-2003, 01:32 PM
  5. deleting a structure from a file
    By spentdome in forum C Programming
    Replies: 4
    Last Post: 05-28-2002, 11:09 AM