Thread: Structure and reading file

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

    Structure and reading file

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    #define MAX 200
    
    
    
    
    void getinfoStudent();
    void prompt();
    
    
    
    
    struct Student
    {
        char firstname[30];
        char lastname[30];
        int studentnumber;
        int markFinal;
        int markMidterm;
        int markAssignment[5];
        int markLab[5];
    
    
    }student[MAX];
    
    
    
    
    
    
    
    
    
    
    int main()
    {
    
    
    prompt();
    
    
    
    
    return 0;
    }
    
    
    
    
    void prompt()
    {
        int choice, flag = 1 ;
        printf("Please choose an option:\n");
        printf("  1. View the information for a specific student\n");
        printf("  2. Add a new student\n");
        printf("  3. Exit\n");
        printf("What is your choice? :");
        scanf("%d",&choice);
    
    
        while(flag)
        {
            switch(choice)
            {
            case 1:
                getinfoStudent();
                break;
            case 2:
    
    
                break;
            case 3:
                exit(0);
                break;
    
    
            default:
            printf("Invalid choice. Please try again.\n");
            printf("Press any key to continue.\n");
            getch();
            system("cls");
            prompt();
            }
        }
    
    
    }
    
    
    
    
    void getinfoStudent()
    {
        system("cls");
        int studentnum, i;
        printf("Please type the student number to look at the student's information: ");
        scanf("%d",&studentnum);
    
    
    
    
    
    
        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("%c %c %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);
            }
            else
            {
                printf("Invalid student number. Please type it again.\n");
                getch();
                system("cls");
                getinfoStudent();
            }
        }
    
    
    }
    
    void printstudentInfo()
    {
    
    
    FILE *fp;
    
    
    fp = fopen("studentinfo.txt","r");
     if (fp == NULL) {
             printf("I couldn't open results.dat for printing.\n");
             exit(0);
          }
    while (fscanf(fp, "%c, %c, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n", &firstname, &lastname,&studentnumber,&markAssignment[0],&markAssignment[1],&markAssignment[2],&markAssignment[3],&markAssignment[4],&markLab[0],&markLab[1],&markLab[2],&markLab[3],&markLab[4],&markMidterm,&markFinal)== 14)
    printf("%c %c %d %d %d %d %d %d %d %d %d %d %d %d %d\n",firstname, lastname,studentnumber,markAssignment[0],markAssignment[1],markAssignment[2],markAssignment[3],markAssignment[4],markLab[0],markLab[1],markLab[2],markLab[3],markLab[4],markMidterm,markFinal);
    
    
    fclose(fp);
    
    
    }
    I made a txt file which contains a necessary information into my project file and tried to read and print it. However, seems like my program is not reading my file at all. Could anyone help me with this problem?

    PS. I named my file as student
    Last edited by tosihiro2007; 03-11-2013 at 03:12 PM.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Your code does not even compile, because you don't declare many of the variables you're reading. I can also tell that, even if they were declared, you're not handling firstname and lastname right; you're scanning and printing them as chars, and I think you want them to be strings of chars instead. Please fix these errors then re-post your code, as well as the contents of your "student" file so that we can see what you're trying to read in.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    40
    Sorry>< I posted wrong code
    Could any mod delete this thread? so I can create new one

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a Structure from a file
    By ferro9ii in forum C Programming
    Replies: 5
    Last Post: 12-07-2012, 07:18 PM
  2. Reading text from file using structure.
    By shawney in forum C Programming
    Replies: 2
    Last Post: 02-07-2012, 02:58 AM
  3. Structure and reading from a file
    By acmarshall in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2011, 03:40 PM
  4. Reading structure from file
    By nime in forum C++ Programming
    Replies: 7
    Last Post: 03-21-2010, 04:17 PM
  5. reading structure from file..??
    By CyC|OpS in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 05:28 AM