Tableau et manipulation d'enregistrement en c

Fermé
nemad - 13 juil. 2020 à 13:29
[Dal] Messages postés 6174 Date d'inscription mercredi 15 septembre 2004 Statut Contributeur Dernière intervention 2 février 2024 - 15 juil. 2020 à 13:02
Boujour ! j'aimerais trier et rechercher un element dans un fichier mais je n'y arrive pas. mon code :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int nombre,i=0;
 int ancien[6], nouveau[6];
char NOM_FICHIER[30], NOM_PERS[30], NOM_SUPPR[30];
// le menu du programme
void menu(){
    char choix[10];int MENU;
    while(choix[0]!='6'){
printf("==========================================\n");
printf("|                MENU                    |\n");
printf("|           1 Inserer lineairement       |\n");
printf("|           2 Inserer a une position     |\n");
printf("|           3 Rechercher un element      |\n");
printf("|           4 Supprimer un element       |\n");
printf("|           5 trier                      |\n");
printf("|      	    6 Afficher                   |\n");
printf("|                                        |\n");
printf(" ========================================");
      
    
        printf("\n");
        printf("\t Votre choix:  ");
        scanf("%s",choix);
        MENU=strlen(choix);
        if(MENU==1){
            switch(choix[0]){
                case '1' :insererl() ;break;
                case '2' :insererp() ;break;
                case '3' :rechercher() ;break;
                case '4' :supprimer() ;break;
                case '5' :trier() ;break;
                case '6' :afficher() ;break;
                
               
                
                default:printf("\n Erreur choix invalide ");system("pause");
            }
        }
    }
}

//insertion linéaire
insererl()
{   FILE *F;
  F = fopen("record.txt", "w");  /* write */
        printf("Nombre d'enregistrements a creer : ");
  scanf("%d", &nombre);
  while (i<nombre)
     { 
      printf("Entrez l'element %d du tableau : ",i);
      scanf("%d",&ancien);
      fprintf(F, "%d\n", *ancien);
      i++;
     }
 fclose(F);
	
}
insererp()
{

}

rechercher()
{   
 FILE *F;
   int echerche;
    F = fopen("record.txt", "r");
    
     scanf("%d",&echerche);
     if(F!=NULL)
    {
      fscanf(F, "%d %d %d %d %d %d", &ancien[0], &ancien[1], &ancien[2],&ancien[3],&ancien[4],&ancien[5]);
      if(strcmp(ancien[i],echerche)==0)
        printf("Les elements du tableau : %d", ancien[i]);
     
        fclose(F); 
    }
    else
    {
       printf("Votre fichier est vide !!!");
    }
   
}

supprimer()
{
	  FILE *F, *OUTFILE;
  /* Ouverture de l'ancien fichier en lecture */
  do
    {
     F = fopen("record.txt", "r"); 
     if (!F) 
         printf("\aERREUR: Impossible d'ouvrir le fichier: %s.\n", ancien);
    }
  while (!F);
  /* Ouverture du nouveau fichier en écriture */
  do
    {
     OUTFILE = fopen("nouveau.txt", "w");
     if (!OUTFILE) 
         printf("\aERREUR: Impossible d'ouvrir "
                "le fichier: %s.\n", nouveau);
    }
  while (!OUTFILE);
  /* Saisie de l'enregistrement à supprimer */
  printf("Enregistrement à supprimer : ");
  scanf("%s",NOM_SUPPR);
  /* Traitement */
  /* Copie de tous les enregistrements à */
  /* l'exception de celui à supprimer.   */
  while (!feof(F))
    {
     fscanf(F, "%s\n", NOM_PERS);
     if (strcmp(NOM_PERS, NOM_SUPPR) != 0)
          fprintf(OUTFILE, "%s\n", NOM_PERS);
    }
  /* Fermeture des fichiers */
  fclose(OUTFILE);
  fclose(F);
   remove("record.txt");
  rename("nouveau.txt","record.txt");
}

void trier()
{
	
}

afficher()
{
 FILE *F;
   
    F = fopen("record.txt", "r");
    
     
     if(F!=NULL)
    {
      fscanf(F, "%d %d %d %d %d %d", &ancien[0], &ancien[1], &ancien[2],&ancien[3],&ancien[4],&ancien[5]);
        printf("Les elements du tableau : %d %d %d %d %d %d", ancien[0], ancien[1], ancien[2], ancien[3], ancien[4],ancien[5]);
 
        fclose(F); 
    }
    else
    {
       printf("Votre fichier est vide !!!");
    }
 
}
main(){ 
  
    menu();
}
A voir également:

1 réponse

[Dal] Messages postés 6174 Date d'inscription mercredi 15 septembre 2004 Statut Contributeur Dernière intervention 2 février 2024 1 083
Modifié le 13 juil. 2020 à 17:09
Salut,

Tu n'expliques pas quel est ton problème.

Alors, pour commencer, le compilateur gcc avec les warnings se plaint de nombreux problèmes, que tu devrais traiter :

$ gcc -Wall 36754754.c
36754754.c: In function ‘menu’:
36754754.c:30:15: warning: implicit declaration of function ‘insererl’; did you mean ‘unsetenv’? [-Wimplicit-function-declaration]
     case '1' :insererl() ;break;
               ^~~~~~~~
               unsetenv
36754754.c:31:15: warning: implicit declaration of function ‘insererp’; did you mean ‘unsetenv’? [-Wimplicit-function-declaration]
     case '2' :insererp() ;break;
               ^~~~~~~~
               unsetenv
36754754.c:32:15: warning: implicit declaration of function ‘rechercher’ [-Wimplicit-function-declaration]
     case '3' :rechercher() ;break;
               ^~~~~~~~~~
36754754.c:33:15: warning: implicit declaration of function ‘supprimer’ [-Wimplicit-function-declaration]
     case '4' :supprimer() ;break;
               ^~~~~~~~~
36754754.c:34:15: warning: implicit declaration of function ‘trier’; did you mean ‘strsep’? [-Wimplicit-function-declaration]
     case '5' :trier() ;break;
               ^~~~~
               strsep
36754754.c:35:15: warning: implicit declaration of function ‘afficher’ [-Wimplicit-function-declaration]
     case '6' :afficher() ;break;
               ^~~~~~~~
36754754.c: At top level:
36754754.c:46:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
 insererl()
 ^~~~~~~~
36754754.c: In function ‘insererl’:
36754754.c:54:11: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int (*)[6]’ [-Wformat=]
   scanf("%d",&ancien);
          ~^  ~~~~~~~
36754754.c: At top level:
36754754.c:61:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
 insererp()
 ^~~~~~~~
36754754.c:66:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
 rechercher()
 ^~~~~~~~~~
36754754.c: In function ‘rechercher’:
36754754.c:76:19: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]
   if(strcmp(ancien[i],echerche)==0)
             ~~~~~~^~~
In file included from 36754754.c:3:
/usr/include/string.h:136:32: note: expected ‘const char *’ but argument is of type ‘int’
 extern int strcmp (const char *__s1, const char *__s2)
                    ~~~~~~~~~~~~^~~~
36754754.c:76:23: warning: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]
   if(strcmp(ancien[i],echerche)==0)
                       ^~~~~~~~
In file included from 36754754.c:3:
/usr/include/string.h:136:50: note: expected ‘const char *’ but argument is of type ‘int’
 extern int strcmp (const char *__s1, const char *__s2)
                                      ~~~~~~~~~~~~^~~~
36754754.c: At top level:
36754754.c:88:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
 supprimer()
 ^~~~~~~~~
36754754.c: In function ‘supprimer’:
36754754.c:96:55: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat=]
    printf("\aERREUR: Impossible d'ouvrir le fichier: %s.\n", ancien);
                                                      ~^      ~~~~~~
                                                      %ls
36754754.c:104:11: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat=]
    printf("\aERREUR: Impossible d'ouvrir "
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      "le fichier: %s.\n", nouveau);
                           ~~~~~~~
36754754.c:105:20: note: format string is defined here
      "le fichier: %s.\n", nouveau);
                   ~^
                   %ls
36754754.c: At top level:
36754754.c:127:6: warning: conflicting types for ‘trier’
 void trier()
      ^~~~~
36754754.c:34:15: note: previous implicit declaration of ‘trier’ was here
     case '5' :trier() ;break;
               ^~~~~
36754754.c:132:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
 afficher()
 ^~~~~~~~
36754754.c:152:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
 main(){
 ^~~~
36754754.c: In function ‘insererl’:
36754754.c:60:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
36754754.c: In function ‘insererp’:
36754754.c:64:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
36754754.c: In function ‘rechercher’:
36754754.c:86:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
36754754.c: In function ‘supprimer’:
36754754.c:125:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
36754754.c: In function ‘afficher’:
36754754.c:151:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^


Lorsque tu auras tenu compte de ces warnings, que tu les auras compris et corrigé le code, tu pourras te pencher sur d'autres problèmes.

Pas nécessairement dans l'ordre et non exhaustivement :

Tu devrais faire un choix 7 pour sortir du programme au lieu du choix 6.

Tu devrais mettre du contenu dans ta fonction de tri et y implémenter un algorithme de tri ou utiliser la fonction standard
qsort()
accessible par l'entête
<stdlib.h>  
:

http://www.cplusplus.com/reference/cstdlib/qsort/.

Il te faudra aussi décider si ton fichier de données doit obligatoirement contenir 6 entiers (auquel cas on ne comprend pas comment on peut ajouter ou insérer des données), ou si le nombre de données qu'il peut contenir est au choix de l'utilisateur (auquel cas le code de lecture des données dans tes fonctions de recherche et d'affichage est à revoir).

Tu devrais faire varier la valeur de
i
en ligne 76.

Dal
0
[Dal] Messages postés 6174 Date d'inscription mercredi 15 septembre 2004 Statut Contributeur Dernière intervention 2 février 2024 1 083
15 juil. 2020 à 13:02
Lorsque tu auras tenu compte de ces warnings, que tu les auras compris et corrigé le code, tu pourras te pencher sur d'autres problèmes.

Si tu ne comprends pas certains de ces warnings après avoir tenté de les comprendre et recherché les raisons de leur signalement, n'hésite pas à nous dire ce que tu ne comprends pas.
0