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>

You need to split the line into individual tokens immately after reading it from the text file, then you can copy the integer portion to n member of the structure and the name portion to the nom structure member. I think fscanf() would be easier to use than fgets()

char nom[80];
int n;
struct S* head = 0;
while( fscanf(f,"%d %s", &n, nom) > 0 )
{

    tete=(S *)malloc(sizeof(S));
    s->n = n;
    strcpy(s->nom,nom);
    AddLinkedList(&head, s);
}

Hello guys,
I just modified the code to sort data in linked list( alphabetic order with the name)
but it won't work like I wish. Could yiu help me, please? Many thanks.

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void main ()
{
struct S{char nom[80];int n; struct S *next,*suiv;}*crt,*tete;
 S *liste=NULL,*nouv=NULL,*courant=NULL,*nn;
int j,i=0;
//struct S e[5];
char tmp[128];
FILE *f;
//static const char filename[] = "c:\\file.txt";
clrscr();
tete=NULL;
f = fopen ( "c:\\client.txt", "r" );
if ( f != NULL )
{
char line[ 128 ];
 liste=(S*)malloc(sizeof(S));
                      fread(liste,sizeof(S),1,f);
                      liste->suiv=NULL;
                      while(!feof(f))
                      {
                             nouv=(S*)malloc(sizeof(S));
                             fread(nouv,sizeof(S),1,f);
                             nouv->suiv=NULL;
                             if(strcmp(liste->nom,nouv->nom)<0)
                             {
                                    if(liste->suiv==NULL)
                                    {
                                       liste->suiv=nouv;

                                    }
                             }
                             else
                             {
                                        nouv->suiv=liste;
                                        liste=nouv;
                             }
                             courant=liste;
                             while(courant->suiv!=NULL&&courant!=nouv)
                             {
                                         if(strcmp(nouv->nom,courant->suiv->nom)<0)
                                         {
                                                nouv->suiv=courant->suiv;
                                                courant->suiv=nouv;
                                         }
                                         courant=courant->suiv;
                             }
                             if(courant!=nouv)
                             {
                                courant->suiv=nouv;
                             }
                  }
          }
          else
                printf("erreur ,fichier inexistant\n");

 crt=liste;
while(crt)
{  printf("%s",crt->nom);
 crt=crt->suiv;  }
fclose (f);

   getch();
}

lines 22 and 27 are wrong -- files can't be read directly into structures like that, unless that was the way the file was written. First read the file into individual variables of the appropriate type, then copy them into the structures. The reason is that the structure contains other members (such as pointers) which are not in the file, so using sizeof() will make the program read the file incorrectly. See the code snippet I posted previously in this thread for example.

Hi guys,
I got another problem with this exercice.
my program works but it has a little prob. in function affiche1Jour()..the application close automatically when I entered the day which I'd to see. Any idea will be appreciated. Please help. Thanks a lot.

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
void menu();
void affiche1Jour();
void afficheAllJours();
void menu()
{
printf("\n*************MENU******************\n");
printf("\n1 affiche 1 jour de Match");printf("\n2 affiche tous les jours de Match");printf("\n 0. Quitter");printf("\nVotre choix: ");
}
//char presentation();


void main ()
{menu();
 int choix;

do{
//menu();
scanf("%d",&choix);
switch(choix)
{
 case 1:  affiche1Jour();break;
case 2:  afficheAllJours();break;
case 3:   exit(0);
default:
          printf("\mauvais choix, veuillez le refaire,svp");
}
}while (!choix);
getch();


 }//void


/*printf(" Choissizez l'option, svp\n");
 printf("1. affiche jour de Match\n");
 printf("2. Quitter\n");
 scanf("%d",&n);

 switch(n)
 {case 1 : affiche();
           break;
  case 2 : exit(0);
 }
 // getch();
//}



void affiche()
{ */


 void affiche1Jour(){
   int j,i=0;
 char *j1 = "jour1"; char *j2 = "jour2";char *j3 = "jour3";char *j4 = "jour4";
char* tJour[4];
char js[10];
tJour[0]= j1;
tJour[1]= j2;
tJour[2]= j3;
tJour[3]= j4;
printf("les jours de Championat saison1  \n");
printf("-----------------------------------\n");

printf("%s\n",tJour[0]);
printf("%s\n",tJour[1]);
printf("%s\n",tJour[2]);
printf("%s\n",tJour[3]);
struct {char jou[10];char dat[12];char stade[15];char nom1[10];char nom2[15];char s1[4];char s2[3];char h[11];char dure[25];}d[16]={
 {"jour1","02/02/2013","stade:Leopold","Spirou-","Liege score:","62-","58","10:00-11:30","duree:1h30"},
 {"jour1","02/02/2013","stade:Leopold","Spirou-","Mons  score:","81-","63","14:00-15:30","duree:1h30"},
 {"jour1","02/02/2013","stade:Leopold","Liege-","Namur  score:","72-","40","18:30-20:10","duree:1h40"},
 {"jour1","02/02/2013","stade:Chaleroi","Liege-","Gand  score:","62-","90","18:30-20:00","duree:1h30"},
 {"jour2","03/02/2013","stade:ANvers","Mons-","Anvers  score:","72-","79","10:00-11:30","duree:1h30"},
 {"jour2","03/02/2013","stade:Leopold","Mons-","Liege  score:","62-","79","10:00-11:30","duree:1h30"},
 {"jour2","03/02/2013","stade:Leopold","Spirou-","Mons  score:","61-","63","14:00-15:30","duree:1h30"},
 {"jour2","03/02/2013","stade:Leopold","Mons-","Namur  score:","72-","66","18:30-20:00","duree:1h30"},
 {"jour3","04/02/2013","stade:Leopold","Spirou-","Brux  score:","62-","58","10:00-11:30","duree:1h30"},
 {"jour3","04/02/2013","stade:Leopold","Brux-","Mons  score:","81-","63","14:00-15:30","duree:1h30"},
 {"jour3","04/02/2013","stade:Leopold","Anvers-","Namur  score:","72-","40","18:30-20:00","duree:1h30"},
 {"jour3","04/02/2013","stade:Gand","Anvers-","Gand  score:","72-","60","18:30-20:00","duree:1h30"},
 {"jour4","05/02/2013","stade:Anvers","Mons-","Anvers  score:","82-","79","10:00-11:30","duree:1h30"},
 {"jour4","05/02/2013","stade:Leopold","Mons-","Anvers  score:","62-","79","10:00-11:30","duree:1h30"},
 {"jour4","05/02/2013","stade:Leopold","Spirou-","Anvers  score:","61-","63","14:00-15:30","duree:1h30"},
 {"jour4","05/02/2013","stade:Leopold","Gand-","Namur  score:","72-","66","18:30-20:00","duree:1h30"}};

  printf("Entrez le jour que vous voulez voir,svp\n");
  gets(js);
  for(i=0;i<16;i++)
  {
   if(strcmp(js,d[i].jou)==0) {
   printf("%s %s %s %s %s %s %s %s\n",d[i].dat,d[i].stade,d[i].nom1,d[i].nom2,d[i].s1,d[i].s2,d[i].h,d[i].dure);
   //break;
   }

  }//for
 }
void afficheAllJours() {
int i;

struct {char jou[25];char dat[25];char stade[25];char nom1[25];char nom2[25];char s1[15];char s2[15];char h[25];char dure[20];}d[16]={
 {"jour1","02/02/2013","stade:Leopold","Spirou","Liege","62-","58","10:00-11:30"," duree:1h30"},
 {"jour1","02/02/2013","stade:Leopold","Spirou","Mons","81-","63","14:00-15:30"," duree:1h30"},
 {"jour1","02/02/2013","stade:Leopold","Liege","Namur","72-","40","18:30-20:10"," duree:1h40"},
 {"jour1","02/02/2013","stade:Chaleroi","Liege","Gand","62-","90","18:30-20:00"," duree:1h30"},
 {"jour2","03/02/2013","stade:ANvers","Mons","Anvers","72-","79","10:00-11:30"," duree:1h30"},
 {"jour2","03/02/2013","stade:Leopold","Mons","Liege","62-","79","10:00-11:30"," duree:1h30"},
 {"jour2","03/02/2013","stade:Leopold","Spirou","Mons","61-","63","14:00-15:30"," duree:1h30"},
 {"jour2","03/02/2013","stade:Leopold","Mons","Namur","72-","66","18:30-20:00"," duree:1h30"},
 {"jour3","04/02/2013","stade:Leopold","Spirou","Brux","62-","58","10:00-11:30"," duree:1h30"},
 {"jour3","04/02/2013","stade:Leopold","Brux","Mons","81-","63","14:00-15:30"," duree:1h30"},
 {"jour3","04/02/2013","stade:Leopold","Anvers","Namur","72-","40","18:30-20:00"," duree:1h30"},
 {"jour3","04/02/2013","stade:Gand","Anvers","Gand","72-","60","18:30-20:00"," duree:1h30"},
 {"jour4","05/02/2013","stade:Anvers","Mons","Anvers","82-","79","10:00-11:30"," duree:1h30"},
 {"jour4","05/02/2013","stade:Leopold","Mons","Anvers","62-","79","10:00-11:30"," duree:1h30"},
 {"jour4","05/02/2013","stade:Leopold","Spirou","Anvers","61-","63","14:00-15:30"," duree:1h30"},
 {"jour4","05/02/2013","stade:Leopold","Gand","Namur","72-","66","18:30-20:00"," duree:1h30"}};

 printf("\n\nLISTE DES JOURS DE CHAMPIONAT SAISON1  \n");
printf("-----------------------------------\n");
for(i=0;i<16;i++)
{
 printf("%s %s %s \n\nEquipe de %s-- %s,score:%s %s,Heure de match:%s,%s\n\n",d[i].jou,d[i].dat,d[i].stade,d[i].nom1,d[i].nom2,d[i].s1,d[i].s2,d[i].h,d[i].dure);
}
}

ps: I just a beginner in c lanquage in few weeks ago so I do not know much things in this language.

Your program has a few compiler errors that needs to be fixed before you can run it, and that may be why it exits unexpectadly.

line 29: There is no such escape sequence as \m

lines 106-121: All those times (second to last entries) are too big to fit into the structure. You need to increase the size of the char arrays so that they can properly fit.

thanks a lot Dragon. Happy new year.

Hi,
I've increased the size of char unfortunately the prob. is still there.

Please tell us what you enter. I don't read whatever language you wrote your program in, so I don't know what it is asking.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.