Help Please, Newbie

This is a discussion on Help Please, Newbie within the C Programming forums, part of the General Programming Boards category; Sorry about the long post, but newbie here. I've been creating a Database in text format, which is so far ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    6

    Help Please, Newbie

    Sorry about the long post, but newbie here.

    I've been creating a Database in text format, which is so far going good.

    Everything i've done so far, is working fine and i'm happy with it.

    The database loads, i can navigate through the files next, previous, first, last.

    But, i need to be able to sort, using qsort, the files by ID#. I was wondering if anyone could help, as i'm stuck.

    I've got a bit of coding from books & web, but can't get it to work.

    There are 10 files (records), in the format surname, firstname, id#, DOB.

    I would like to sort all 10 records via the id#, which is of the format 00000000, where the first 2 digits are the year. 01 = 2001, 99 = 1999.

    Also would like the other details, name, etc displayed on the same line, but after the id#.

    I would like to keep the code i have already, which i will provide below.

    Could someone please help me with the code to use. It should work under void goto_sort, S key.

    I hope its not too much to ask.

    As i can't get the sort coding working from what i already have, i have removed this from the code.

    Thanks.

    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 menu();
     
    struct stype
        { 
            char s1[100]; 
            char s2[100]; 
            char s3[100]; 
            char s4[100]; 
        };
    
    struct stype studs[10];
     
        FILE *f;
    
        char *t;
        char seps[] = ",";
        char line[80];
        char page[100][80]; 
       
        int CurrentLine = 0;
        int LastLine = 0;
        int k;
      
        void breakup(int c) 
        {
         // fill up studs[c]
        }
     
    void main() 
        {
        
        FILE *f;
         int len; 
         int r_num = 1;
         int count = 0;
     
         f = fopen("C:\\Students.txt", "r");
         
        if (f !=NULL) 
            { 
                printf("\nfile Opened Ok\n"); 
            }
         else 
            { 
                printf("\nError"); 
                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);
            printf("\n[%s]", line);
            }
        
            breakup(count);
            count++;
            printf("\n\nEND\n");
            printf("\n\n");
            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 'x':
            case 'X':
                finish = 1;
                break;
              
            default:
                printf("\n\n<WRONG CHAR>\n");
            }
        } while (finish!=1);
    }    
         
    void goto_first()
    {
         CurrentLine = 0;
              
         f = fopen("C:\\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("C:\\Students.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("C:\\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("C:\\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; 
        }
    
    }
    Apologies if this type of thread isn't allowed.
    Last edited by chris2k5; 01-28-2005 at 02:48 PM.

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    why don't you post the sorting code you couldn't get to work and specific questions / errors associated with that. I believe that will elicit more responses.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    Thanks for replying mate.

    As you requested, here is the code.

    If i use the code seperately, it will sort by id#, but only id# is shown, and 00 will be at the top rather than at bottom.

    But, when i try to implement this code into the code i featured above, just doesn't work properly, and just need some advice.

    Also trying to get all data displayed on same line.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    
    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);
    }
    
    void goto_sort()
    {
    	 int CurrentLine = 0;
    	 int size;
    	 char line[80];
    	 char* t;
    	 char* seps = ",";
    	 int k = 0;
    	 FILE* f = fopen("C:\\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;
    	}
    	fclose(f);
    	printf("before: \n");
    	for (k=0;k< CurrentLine;k++) 
    	{ 
    		printf("\n[%4d|%s]", k, studs[k].s2); 
    	}
    
    	qsort((void *) studs, CurrentLine, sizeof( struct stype), 
    
    compare);
    
    	printf("\nafter: \n");
    	for (k=0;k< CurrentLine;k++) 
    	{ 
    		printf("\n[%4d|%s]", k, studs[k].s2); 
    	}
    	 
    	 if (CurrentLine >= 10) 
    	 { 
    		 printf("\t   ***END OF RECORDS***   \n\n");
    	 }
    
    }
    
    int main()
    {
    	goto_sort();
    	return 0;
    }
    Its just recking my head. Many thanks again.

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    Stop posting with double spaces please, it is utterly annoying.

  5. #5
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Dude! This guy has 2 posts, he's used code tags, he's shown his code, he's made an effort and he's listened to the advice of others. Give him a bit of breathing space before you start picking on double spaces.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  6. #6
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Code:
    struct stype 
    { 
        char s1[100]; // <-- Is that the id#?
        char s2[100]; 
        char s3[100]; 
        char s4[100]; 
    };

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    I've managed to get the fields showing now after implementing the coding.

    The only problem, is that they still display with 01 first. Any ideas.

    Believe i need to edit the compare.

  8. #8
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Here's my cheap way of getting things to work lol:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    
    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)
    {
       int n1 = atoi(((const struct stype*)p1)->s1);
       int n2 = atoi(((const struct stype*)p2)->s1);
       if(n2 > n1)
          return -1;
       else if(n2 < n1)
          return 1;
       else
          return 0;
    }
    
    void goto_sort()
    {
       int CurrentLine = 0;
       int size;
       char line[80];
       char* t;
       char* seps = ",";
       int k = 0;
       FILE* f = fopen("Students.txt", "r");
       if( !f )
       {
             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;
       }
       fclose(f);
       printf("before: \n");
       for (k=0;k< CurrentLine;k++)
       {
          printf("\n[%4d|%s]", k, studs[k].s1);
       }
    
       qsort((void *) studs, CurrentLine, sizeof( struct stype), compare);
    
       printf("\nafter: \n");
       for (k=0;k< CurrentLine;k++)
       {
          printf("\n[%4d|%s]", k, studs[k].s1);
       }
    
       if (CurrentLine >= 10)
       {
             printf("\t   ***END OF RECORDS***   \n\n");
       }
    
    }
    
    int main( void )
    {
       goto_sort( );
       return 0;
    }
    ... I'm not totally sure if that's what you wanted

  9. #9
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by Azuth
    Dude! This guy has 2 posts, he's used code tags, he's shown his code, he's made an effort and he's listened to the advice of others. Give him a bit of breathing space before you start picking on double spaces.
    I was being nice.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    I tried your coding but didn't do what i want.

    I think the solution i'm looking for, is somehow tell the code that if the first 2 characters of the ID # are 90's, to add 19, giving 1990.

    If any 00's, add 20 giving 2000.

    This should then sort the records with 97 (for example) at the top of the list, and as you go down the list 98, 99, 00 and so on, displayed underneath.

    Is this possible, and would it still be able to display only 99, rather than 1999. If you follow.

    Sand Man, sorry about the double spacing, i copied the coding direct from the c file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 06:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 11:53 AM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21