Click here to Skip to main content
14,387,532 members
Rate this:
Please Sign up or sign in to vote.
See more:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
	struct details{
	char title[256] ;
	char platform [256];
	char score[256];
	char release_year[256];
}record[20000],temp;
int main ()
{
	int i =0,j=0,val=0;
	
  FILE * myfile;
  char a[512];
  
  myfile=fopen ("ign.txt","r");
 FILE *newFile = fopen("ign_new.txt", "w");

  if (myfile==NULL){
   perror (" empty file ");}
  else
  {
     	while ( fgets ( a, sizeof a, myfile ) != NULL ){
      
	  	switch(i) {
     

	   
   case 0  :
   	    strcpy(record[j].title,a);
    
        i++;
        break; 
	
	 case 1  :
	    
		
       strcpy(record[j].platform,a);
    
	   i++;
       break; 
	
	 case 2  :
   	     	   
         	     
        strcpy(record[j].score,a);
         
        i++;
        break; 
	
	 case 3  :
	 	strcpy(record[j].release_year,a);
       
	    i++;
        break; 
	
	 
    case 4:
	    i=0;
	    strcpy(record[j].title,a);
	    j++;
	    break;
	     
       
      
	  
	  
	  
		

}

	
	    
      
    }
    
     printf("%s",record[1].release_year);
     

    int n = j;
     
    
	for(i=1;i<n;i++){
		int flag=0;
		
		for( j=0;j<n-i;j++){
			if(atoi(record[j].score)<atoi(record[j+1].score)){
				strcpy(temp.title,record[j].title);
				strcpy(record[j].title,record[j+1].title);
				strcpy(record[j+1].title,temp.title);
				strcpy(temp.release_year,record[j].release_year);
				strcpy(record[j].release_year,record[j+1].release_year);
				strcpy(record[j+1].release_year,temp.release_year);
				strcpy(temp.platform,record[j].platform);
				strcpy(record[j].platform,record[j+1].platform);
				strcpy(record[j+1].platform,temp.platform);
				strcpy(temp.score,record[j].score);
				strcpy(record[j].score,record[j+1].score);
				strcpy(record[j+1].score,temp.score);
				flag=1;
			}
			
		}
		if(flag==0)break;
	}
       
    
    
    
    
    
    
    

    
    
    
    printf("\n %s",record[1].release_year);
    fclose (myfile);
    
  }
  return 0;
}






/*my file contains 18000 entries and some of them are
title
platform
Score
release_year

140
PC
8
2013

300
Wireless
8
2007

1942
Game Boy Color
5
2000

1942
Wireless
5
2006
*/

What I have tried:

this is not storing the data of score column in record[j].score after sorting but before it is storing correctly
Posted
Updated 27-Oct-18 14:36pm
v4
Comments
Patrice T 28-Oct-18 6:36am
   
"this is not storing the data of score column in record[j].score after sorting but before it is storing correctly"
What make you think it is the problem ?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

If I were you, I would do make a little set of functions to deal with your data structure. It will clean up the code A LOT! I think you will be able to see logic errors much easier. Here are some examples :
int ReadRecord( details * pd, FILE * pf )
{
    char a[512] = { 0 };
    int i = 0;
    while( ( i < 4 ) && fgets( a, sizeof a, pf ) )
    {
        if( ( a[0] == '\r' ) || ( a[0] == '\n' ) )
            continue;  // skip empty lines
        switch( i )
        {
            case 0  :
                strcpy( pd->title, a );
                break; 
            case 1  :
                strcpy( pd->platform, a );
                break; 
            case 2  :
                strcpy( pd->score, a );
                break;
            case 3  :
                strcpy( pd->release_year, a );
                break;
        }
        ++i;
    }
    return i;
}       

void CopyRecord( details *pddest, details *pdsource )
{
    strcpy( pddest->title,        pdsource->title );
    strcpy( pddest->platform,     pdsource->platform );
    strcpy( pddest->score,        pdsource->score );
    strcpy( pddest->release_year, pdsource->release_year );
}

void SwapRecords( details *pda, details *pdb )
{
    details temp = { 0 };
    CopyRecord( temp, pda );
    CopyRecord( pda, pdb );
    CopyRecord( pdb, temp );
}

int main()
{
   int j = 0;

   FILE * newFile = fopen( "ign_new.txt", "w" );
   FILE * myfile = fopen( "ign.txt", "r" );
   if( myfile == NULL )
   {
       perror (" empty file ");
       return -1;
   }

   // read the file

   while( ReadRecord( &records[j], myfile ) )
   {
       ++j;
   }

   fclose( myfile );
   myfile = NULL;

   printf( "read %d records\n", j );

   // sort the records

   int flag;
   int n = j;
   for( i = 1; i < n; i++ )
   {
      flag = 0;
      for( j = 0; j < n - i; j++ )
      {
          if( atoi( record[j].score ) < atoi( record[j+1].score ) )
          {
              SwapRecords( &record[j], &record[j+1] );
              flag = 1;
          }
      }
      if( flag == 0 )
          break;
   }

   // probably should do something with the records here
   // like write them to the new file.g

   fclose( newFile );
   return 0;
}
I would advise making a WriteRecord function that works in reverse of the ReadRecord so that they can handle each other's input/output. Remember to write the blank line in WriteRecord. ;)

By the way - I haven't evaluated your logic. I have left that up to you. My point was to make functions for sequences of logic that deal with the data structure. This can simplify the processing logic and then you can see its inner workings much more clearly.

One hint - I think the terms of the for loops used in the sorting are wrong.

What the heck. Here's how I would do the sort. Often when sorting is involved a compare function is implemented for the data being compared. Here's one for this data structure :
int CompareRecords( details *pda, details * pdb )
{
    // return true if record a's score is less than record b's
    return atoi( pda->score ) < atoi( pdb->score );
}
Now you can write a sort function like this :
void SortRecords( details * pd, int count )
{
    int i, j;

    for( j = 0; j < count - 1; ++j )
    {
         // The highest score record floats to the top so we don't have to check
         // the top any more -> start from the one after the outer loop index.

         for( i = j + 1; i < count; ++i )
         {
              if( CompareRecords( &pd[j], &pd[i] ) )
                  SwapRecords( &pd[j], &pd[i] );
         }
    }
}
Note - I haven't tried this code at all. You can either try it and debug it or use what you want from it.
   
v7
Comments
Rick York 28-Oct-18 0:20am
   
I just noticed you wrote that you had 18000 entries in the file. Yikes! To help the sorting I would make the following changes.
struct details
{
    char title[256];
    char platform [256];
    char score[256];
    char release_year[256];
    int intscore;
};

void CopyRecord( details *pddest, details *pdsource )
{
    memcpy( pddest, pdsource, sizeof( details ) );
}

int CompareRecords( details *pda, details * pdb )
{
    // return true if record a's score is less than record b's
    return pda->intscore < pdb->intscore;
}

// change ReadRecord to have this in case 2 :

            case 2  :
                strcpy( pd->score, a );
                pd->intscore = atoi( a );
                break;
The swap and sort functions do not need to change. This will really speed up the sort because it won't have to convert the score from a string to an integer so many times in the comparison - it can directly compare the two integer values.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100