Hi,
I'm a beginner in C..please help. I'd like to get data from a textfile then store in a linked list and sort them by alphabetical order with the name and numeric with the number.My file like this:
file.txt
16 Turin
13 Dingdong
14 Venise
12 Mud-Ca tha
02 Florence
09 Srinoi
03 Kennos
08 Danang
15 Molino
11 Bagdad
04 Vervines
10 Tocarita
05 Tinto
06 Hello ma bella
07 Merds
01 Taritata
I'd like to have the sorting output in 2 different ways in numeric(number) and alphabetic order with the name. I wrote a code like this one ...but it did not give me the output like I wish..I don't know what i did wrong..please help...
many thanks.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
void main ()
{
struct S{char nom[20];int n; struct S *next;}*crt,*tete;
int j,i=0;
//struct S e[5];
char tmp[7];
FILE *f;
//static const char filename[] = "c:\\file.txt";
clrscr();
tete=NULL;
f = fopen ( "c:\\file.txt", "r" );
if ( f != NULL )
{
char line[ 128 ];
while ( fgets ( line, sizeof line, f ) != NULL ) /* read a line */
{//if(tete==NULL){
tete=(S *)malloc(sizeof(S));
strcpy(tete->nom,line);
tete->next=crt;
crt=tete;
//num=((line[0]-48)*10+(line[1]-48)); //printf("%d\n",num);
//tete->n=num;
//fputs ( line, stdout ); /* ecrit lignes */
}
while ( crt->next ) { //
if(strcmp(crt->nom,crt->next->nom)>0)
{
strcpy ( tmp, crt->nom ) ;
strcpy ( crt->nom,crt->next->nom ) ;
strcpy ( crt->next->nom,tmp ) ;
} crt = crt->next;}
crt=tete;
while(crt)
{ printf("%s\n",crt->nom);
crt=crt->next;
fclose (f);
} }
else
{
printf("fichier vide ou n'extiste pas ");
}
getch();
}
</code>