Thread: help with this

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    5

    Question help with this

    i have an assignment that has to show and sort a student record system from a txt file, i have done this, but it needs to be more efficent an also i need the sort to work properly and be in more depth e.g. alphabetically or some other form

    here it is
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>
     
    void goto_first();
    void goto_prev();
    void goto_next();
    void goto_last();
    void goto_sort();
    void menu();
     
    struct stype
        { 
            char s1[100]; 
            char s2[100]; 
            char s3[100]; 
            char s4[100]; 
        };
    
    struct stype studs[10];
    
    int compare(const void* p1, const void* p2)
    {
        const struct stype* s1 = (const struct stype*)p1;
        const struct stype* s2 = (const struct stype*)p2;
        return strcmp(s1->s3, s2->s3);
    }
    
        FILE *f;
    
        char *t;
        char seps[] = ",";
        char line[80];
        char page[100][100]; 
       
        int len;
        int CurrentLine = 0;
        int LastLine = 0;
        int k;
        int size;
      
        void breakup(int c) 
        {
         // fill up studs[c]
        
        }
     
    void main() 
        {
        
        FILE *f = fopen("studentl.txt", "r");
         
        if (f != NULL) 
            { 
                printf("\n\t\t\t***Student Record System***\n");
                printf("\nFILE STATUS:  File Opened OK\n\n");
            }
         else 
            { 
                printf("\n\t\t\t***STUDENT DATABASE***\n");
                printf("\nFILE STATUS:  ERROR - File not loaded\n");
                printf("\nMake sure the file exe and the student record system are saved together in the same place.\n\n");
                exit(0);
            }
     
         while(!feof(f)) 
            {
            for (k=0; k<1; k++)
            {
            fgets(line, 80, f);
            }
             if (feof(f))
            {
                break;
            }
            len = strlen(line);
            
            if (len > 0) 
            { 
                line[len-1] =0; 
            }
                
            strcpy(page[LastLine++], line);
            
        }
            menu();
     
        }
     
    void menu()
    {
        
        char input;
        char finish = 0;
                
        do 
        {
            printf("Press, N for Next, P for Previous, F for First, or L for Last record.\nPress S, 
    
    to Sort records, by Student #.  X to Exit:\n\n");
            input = getch();
                 
            switch(input)    
            {
              
            case 'f':
            case 'F':
                goto_first();
                break;
            
            case 'l':
            case 'L':
                goto_last();
                break;
              
            case 'n':
            case 'N':
                goto_next();
                break;
    
            case 'p':
            case 'P':
                goto_prev();
                break;
    
            case 's':
            case 'S':
                goto_sort();
                break;
    
            case 'x':
            case 'X':
                finish = 1;
                break;
              
            default:
                printf("\n\n<WRONG CHAR>\n");
            }
        } while (finish!=1);
    }    
         
    void goto_first()
    {
         CurrentLine = 0;
              
         f = fopen("Students.txt", "r");
         if (f !=NULL)
         
    
         for (k=0;k<1;k++)
         {
             fgets(line,80,f);
         }
         
         t=strtok(line,seps); strcpy (studs->s1, t);
         t=strtok(NULL,seps); strcpy (studs->s2, t);
         t=strtok(NULL,seps); strcpy (studs->s3, t);
         t=strtok(NULL,seps); strcpy (studs->s4, t);
    
         printf("\n\t   ***FIRST RECORD***   ");
         printf("\n\nRECORD # %d", k);
         printf("\n\n\tSURNAME: \t%s \n\n\tFIRSTNAME: \t%s \n\n\tSTUDENT #: \t%s\n\n\tD.O.B: \t\t%s\n", 
    
    studs->s1, studs->s2, studs->s3, studs->s4);
         printf("\n\n");;
    
         printf( "\t***BEGINNING OF RECORDS***\n\n");
    
         CurrentLine = 1;
    }
    
    void goto_last()
    {
         CurrentLine = 9;
         
         f = fopen("student.txt", "r");
         if (f !=NULL)
         
         for (k=0;k<10;k++)
         {
             fgets(line,80,f);
         }
         
         t=strtok(line,seps); strcpy (studs->s1, t);
         t=strtok(NULL,seps); strcpy (studs->s2, t);
         t=strtok(NULL,seps); strcpy (studs->s3, t);
         t=strtok(NULL,seps); strcpy (studs->s4, t);
    
         printf("\n\t   ***LAST RECORD***   ");
         printf("\n\nRECORD # %d", k);
         printf("\n\n\tSURNAME: \t%s \n\n\tFIRSTNAME: \t%s \n\n\tSTUDENT #: \t%s\n\n\tD.O.B: \t\t%s\n", 
    
    studs->s1, studs->s2, studs->s3, studs->s4);
         printf("\n\n");;
    
         printf( "\t  ***END OF RECORDS***  \n\n");
    }
     
    void goto_next()
    {
         
        while (CurrentLine != 10)
        {
         CurrentLine = CurrentLine + 1;
    
         f = fopen("Students.txt", "r");
         if (f !=NULL)
         
         for (k=0;k<CurrentLine;k++)
         {
             fgets(line,80,f);
         }
         
         t=strtok(line,seps); strcpy (studs->s1, t);
         t=strtok(NULL,seps); strcpy (studs->s2, t);
         t=strtok(NULL,seps); strcpy (studs->s3, t);
         t=strtok(NULL,seps); strcpy (studs->s4, t);
    
         printf("\n\t   ***NEXT RECORD***   ");
         printf("\n\nRECORD # %d", k);
         printf("\n\n\tSURNAME: \t%s \n\n\tFIRSTNAME: \t%s \n\n\tSTUDENT #: \t%s\n\n\tD.O.B: \t\t%s\n", 
    
    studs->s1, studs->s2, studs->s3, studs->s4);
         printf("\n\n");;     
         
         if (CurrentLine >= 10) { printf( "\t  ***END OF RECORDS***  \n\n");}
    
         return;
         
        }
    
    }
     
    void goto_prev()
    {
         while (CurrentLine != 1)
        {
         
         CurrentLine = CurrentLine - 1;
    
         f = fopen("Students.txt", "r");
         if (f !=NULL)
         
         for (k=0;k<CurrentLine;k++)
         {
             fgets(line,80,f);
         }
    
         t=strtok(line,seps); strcpy (studs->s1, t);
         t=strtok(NULL,seps); strcpy (studs->s2, t);
         t=strtok(NULL,seps); strcpy (studs->s3, t);
         t=strtok(NULL,seps); strcpy (studs->s4, t);
    
         printf("\n\t   ***PREVIOUS RECORD***   ");
         printf("\n\nRECORD # %d", k);
         printf("\n\n\tSURNAME: \t%s \n\n\tFIRSTNAME: \t%s \n\n\tSTUDENT #: \t%s\n\n\tD.O.B: \t\t%s\n", 
    
    studs->s1, studs->s2, studs->s3, studs->s4);
         printf("\n\n");;     
    
         if (CurrentLine <= 1) { printf("\t ***BEGINNING OF RECORDS***\n\n");}
         
         return; 
        }
    
    }
    
    void goto_sort()
    {
         int CurrentLine = 0;
         
         FILE* f = fopen("Students.txt", "r");
         if(f == NULL)
         {
             printf("Can't open file\n");
             return;
         }
    
        while(fgets(line,sizeof(line),f) != NULL)
        {
            size = strlen(line);
            if(line[size-1] == '\n')
                line[size-1] = 0;
            t=strtok(line,seps); strcpy (studs[CurrentLine].s1, t);
            t=strtok(NULL,seps); strcpy (studs[CurrentLine].s2, t);
            t=strtok(NULL,seps); strcpy (studs[CurrentLine].s3, t);
            t=strtok(NULL,seps); strcpy (studs[CurrentLine].s4, t);
            CurrentLine++;
            if(CurrentLine == 10)
                break;
        }
        
        printf("BEFORE SORTING: \n\n");
        printf("\t RECORD | STUDENT # | D.O.B\t | SURNAME\t | FIRSTNAME\n");
        for (k=0;k< CurrentLine;k++) 
        { 
            printf("\n\t%4d\t| %s  | %s\t | %s\t | %s ", k, 
            studs[k].s3, 
            studs[k].s4, 
            studs[k].s1, 
            studs[k].s2); }
    
        qsort((void *) studs, CurrentLine, sizeof( struct stype), compare);
    
        printf("\n\nAFTER SORTING: \n\n");
        printf("\t RECORD | STUDENT # | D.O.B\t | SURNAME\t | FIRSTNAME\n");
        for (k=0;k< CurrentLine;k++) 
        { 
            printf("\n\t%4d\t| %s  | %s\t | %s\t | %s ", k, 
            studs[k].s3, 
            studs[k].s4, 
            studs[k].s1, 
            studs[k].s2); 
        }
         
         if (CurrentLine >= 10) 
         { 
             printf("\n\n\t\t\t   ***END OF RECORDS***   \n\n");
         }
    
    }
    any help is much appreciated thanks guys
    Last edited by tyrantil; 01-30-2005 at 01:46 PM. Reason: missed stuff out

Popular pages Recent additions subscribe to a feed