Click here to Skip to main content
15,893,594 members

How do I sort a file in C

Member 14009930 asked:

Open original thread
#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
Tags: C, Sorting, FileIO

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900