Lecture fichier

Fermé
cs_perles Messages postés 74 Date d'inscription samedi 7 juillet 2007 Statut Membre Dernière intervention 29 mars 2015 - Modifié par Whismeril le 27/03/2015 à 07:34
Whismeril Messages postés 19022 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 17 avril 2024 - 27 mars 2015 à 07:38
Bonjour,

J'obtiens le message suivant Process returned -1073741819 et le programme plante. Mon fichier CBLEtilab.ini contient :
action=ADDPAT
id_med=9997
user=125784b92703c
code_labo=0
open=1

Comment est-ce que je peux faire ?

ça fait une semaine que je suis dessus et je ne trouve pas de solutions.

MERCI



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

int main()
{
    int compteur;
    char action [ 128 ];
    char id_med [ 128 ];
    char user [ 128 ];
    char code_labo [ 128 ];
    char open [ 128 ];

    FILE * file;
    char filename[] = "c:\\etilab\\import\\CBLEtilab.ini";

   compteur = -1;
 //*  static const char filename[] = "c:\\etilab\\import\\CBLEtilab.ini";
 //*  FILE *file = fopen ( "c:\\etilab\\import\\CBLEtilab.ini", "r" );
   file = fopen("c:\\etilab\\import\\CBLEtilab.ini", "r");

   if ( file != NULL )
//*   while( !feof(file))
   {
      char line [ 128 ]; /* or other suitable maximum line size */


      while ( compteur != 4 ) /* read a line */
      {
         fgets ( line, sizeof line, file );
         printf("Ligne : %s", line); /* write the line */
       ++compteur;
       if (compteur == 0)
        {
               action[ 128 ] = line[ 128 ];
               printf("L0 : %s", line);
        }
       if (compteur == 1)
        {
               id_med[ 128 ] = line[ 128 ];
               printf("L1 : %s", line);
        }

       if (compteur == 2)
        {
               user[ 128 ] = line[ 128 ];
               printf("L2 : %s", line);
        }
       if (compteur == 3)
        {
               code_labo[ 128 ] = line[ 128 ];
               printf("L3 : %s", line);
        }
       if (compteur == 4)
        {
               open[ 128 ] = line[ 128 ];
               printf("L4 : %s", line);
        }
     }
   }
      fclose ( file );

    return 0;
}

6 réponses

cptpingu Messages postés 3837 Date d'inscription dimanche 12 décembre 2004 Statut Modérateur Dernière intervention 28 mars 2023 123
17 mars 2015 à 10:59
Comment est-ce que je peux faire ?

Comme déjà dit dans de précédents échanges, il te faut apprendre les bases du langage. Là, on sent que tu ne les a pas et que tu ne comprends pas du tout ce que tu fais.
Tu as l'air de copier coller des morceaux de code d'un peu partout, en espérant que ça finisse par fonctionner sur un malentendu...

Techniquement, truc[128] = line[128] est la base de ton souci. Accéder à la case 128 dans un tableau de taille 128 qui commence à 0, plante (la dernière case est 127). De plus, ce n'est pas comme cela qu'on copie une chaine dans une autre, là tu ne copierais qu'un seul caractère. Bref, ton souci premier est d'apprendre le langage, et tant que tu ne feras pas ça, tu resteras bloqué une semaine, deux semaines, 1 an même, mais tu ne pourras pas avancer.

__________________________________________________________________________________________________

Améliorez votre expérience CodeS-SourceS avec ce plugin:
http://codes-sources.commentcamarche.net/forum/affich-10000111-plugin-better-cs-2#cptpingu-signature
0
cs_perles Messages postés 74 Date d'inscription samedi 7 juillet 2007 Statut Membre Dernière intervention 29 mars 2015
Modifié par Whismeril le 26/03/2015 à 11:21
Bonjour,

J'ai modifié mon code comme ci-dessous et cela fonctionne maintenant.

#include <stdlib.h>
#include <stdio.h>
#define TAILLE_MAX 1000

int main(int argc, char *argv[])
{
    FILE* fichier = NULL;
    int compteur;
    char chaine[TAILLE_MAX] = "";
    char action[TAILLE_MAX] = "";
    char id_med[TAILLE_MAX] = "";
    char user[TAILLE_MAX] = "";
    char code_labo[TAILLE_MAX] = "";
    char open[TAILLE_MAX] = "";

    fichier = fopen("c:\\etilab\\import\\CBLEtilab.ini", "r");
    compteur = -1;

    if (fichier != NULL)
    {
        while (fgets(chaine, TAILLE_MAX, fichier) != NULL) // On lit le fichier tant qu'on ne reçoit pas d'erreur (NULL)
        {
            ++compteur;
  //*            printf("%s", chaine); // On affiche la chaîne qu'on vient de lire
            if (compteur == 0)
            {
                   strcpy(action, chaine);
  //*                 printf("A : %s", action);
            }
            if (compteur == 1)
            {
                   strcpy(id_med, chaine);
  //*                   printf("I : %s", id_med);
  //*                  printf("C : %s", chaine);
            }
            if (compteur == 2)
            {
                   strcpy(user, chaine);
  //*                   printf("U : %s", user);
  //*                   printf("C : %s", chaine);
            }
            if (compteur == 3)
            {
                   strcpy(code_labo, chaine);
  //*                   printf("O : %s", code_labo);
  //*                   printf("C : %s", chaine);
            }
            if (compteur == 4)
            {
                   strcpy(open, chaine);
  //*                   printf("P : %s", open);
  //*                   printf("C : %s", chaine);
            }
        }
        printf("A : %s", action);
        printf("I : %s", id_med);
        printf("U : %s", user);
        printf("O : %s", code_labo);
        printf("P : %s", open);
        fclose(fichier);
    }

    return 0;
}


Merci et bonne journée

A bientôt
0
Whismeril Messages postés 19022 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 17 avril 2024 656
26 mars 2015 à 11:22
Bonjour, pour la coloration syntaxique, pense à spécifier le langage, voir ici.
0
cs_perles Messages postés 74 Date d'inscription samedi 7 juillet 2007 Statut Membre Dernière intervention 29 mars 2015
26 mars 2015 à 23:54
Bonjour,

J'ai modifié mon code comme ci-dessous et cela fonctionne maintenant.

#include <stdlib.h>
#include <stdio.h>
#define TAILLE_MAX 1000

int main(int argc, char *argv[])
{
    FILE* fichier = NULL;
    
    int compteur;
    int v;
    
    char Explorer[TAILLE_MAX] = "";
    char ExplorerAdd[TAILLE_MAX] = "&";
    char Explorer1[TAILLE_MAX] = "https://www.proxilis.ch/directlab/link_doc.php?";
    char ExplorerNum[TAILLE_MAX] = "num=";
    char ExplorerNom[TAILLE_MAX] = "nom=";
    char ExplorerPreNom[TAILLE_MAX] = "prenom=";
    char ExplorerSexe[TAILLE_MAX] = "sexe=";
    char ExplorerNaissance[TAILLE_MAX] = "naissance=";
    char ExplorerAdresse[TAILLE_MAX] = "adresse=";
    char ExplorerCP[TAILLE_MAX] = "cp=";
    char ExplorerVille[TAILLE_MAX] = "ville=";
    char ExplorerPays[TAILLE_MAX] = "pays=";
    char ExplorerTel[TAILLE_MAX] = "tel=";
    char ExplorerMobile[TAILLE_MAX] = "mobile=";
    char ExplorerEmail[TAILLE_MAX] = "email=";

    char action[TAILLE_MAX] = "";
    char id_med[TAILLE_MAX] = "";
    char user[TAILLE_MAX] = "";
    char code_labo[TAILLE_MAX] = "";
    char open[TAILLE_MAX] = "";

    char chaine[TAILLE_MAX] = "";
    char chaineT[TAILLE_MAX] = "";
    char num[TAILLE_MAX] = "";
    char nom[TAILLE_MAX] = "";
    char prenom[TAILLE_MAX] = "";
    char sexe[TAILLE_MAX] = "";
    char sexenum[TAILLE_MAX] = "";
    char naissance[TAILLE_MAX] = "";
    char jjnaissance[2] = "";
    char mmnaissance[2] = "";
    char aanaissance[2] = "";
    char adresse[TAILLE_MAX] = "";
    char cp[TAILLE_MAX] = "";
    char ville[TAILLE_MAX] = "";
    char pays[TAILLE_MAX] = "";
    char tel[TAILLE_MAX] = "";
    char mobile[TAILLE_MAX] = "";
    char email[TAILLE_MAX] = "";
    char Reseau[TAILLE_MAX] = "";
    char *ptr = "";
    char *ptr3000;
    char *ptr3101;
    char *ptr3102;
    char *ptr3103;
    char *ptr3106;
    char *ptr3107;
    char *ptr3110;
    char *str1 = "";
    char *str2 = "3000";
    char *str3000 = "3000";
    char *str3101 = "3101";
    char *str3102 = "3102";
    char *str3103 = "3103";
    char *str3106 = "3106";
    char *str3107 = "3107";
    char *str3110 = "3110";
    char *resultat3000 = "";
    char *sexefille = "2";
    char *sexegarcon = "1";
    char *new_s = NULL;
    char *new_ville = NULL;
    char *new_cp = NULL;
    char *new_jj = NULL;
    char *new_mm = NULL;
    char *new_aa = NULL;
    unsigned int start;
    unsigned int end;
    
/* Ouverture du fichier CBLEtilab.ini */
    fichier = fopen("c:\\etilab\\import\\CBLEtilab.ini", "r");
    compteur = -1;

    if (fichier != NULL)
    {
        while (fgets(chaine, TAILLE_MAX, fichier) != NULL) // On lit le fichier tant qu'on ne reçoit pas d'erreur (NULL)
        {
            ++compteur;
  //*            printf("%s", chaine); // On affiche la chaîne qu'on vient de lire
            if (compteur == 0)
            {
                   strcpy(action, chaine);
  //*                 printf("A : %s", action);
            }
            if (compteur == 1)
            {
                   strcpy(id_med, chaine);
  //*                   printf("I : %s", id_med);
  //*                  printf("C : %s", chaine);
            }
            if (compteur == 2)
            {
                   strcpy(user, chaine);
  //*                   printf("U : %s", user);
  //*                   printf("C : %s", chaine);
            }
            if (compteur == 3)
            {
                   strcpy(code_labo, chaine);
  //*                   printf("O : %s", code_labo);
  //*                   printf("C : %s", chaine);
            }
            if (compteur == 4)
            {
                   strcpy(open, chaine);
  //*                   printf("P : %s", open);
  //*                   printf("C : %s", chaine);
            }
        }
        printf("%s", action);
        printf("%s", id_med);
        printf("%s", user);
        printf("%s", code_labo);
        printf("%s", open);
/* Fermeture du fichier */
        fclose(fichier);
    }
/* Ouverture du fichier GDTEtilab.GDT */
    fichier = fopen("c:\\etilab\\import\\GDTEtilab.GDT", "r");
    compteur = -1;

    if (fichier != NULL)
    {
        while (fgets(chaine, TAILLE_MAX, fichier) != NULL) // On lit le fichier tant qu'on ne reçoit pas d'erreur (NULL)
        {
            ++compteur;
 //*           printf("%s", chaine); // On affiche la chaîne qu'on vient de lire
            strcpy(str1, chaine);
 //*            printf("%s", str1);
/* CHAINE */
            new_s = NULL;
            start = 7;
            end = 100;
            if (chaine != NULL && start < end)
            {
/* (1)*/
               new_s = malloc (sizeof (*new_s) * (end - start + 2));
               if (new_s != NULL)
               {
                  int i;

/* (2) */
                  for (i = start; i <= end; i++)
                  {
/* (3) */
                     new_s[i-start] = chaine[i];
                  }
                  new_s[i-start] = '\0';
               }
               else
               {
                  fprintf (stderr, "Memoire insuffisante\n");
                  exit (EXIT_FAILURE);
               }
            }
//*            printf("%s\n", new_s);
/* VILLE */
            new_ville = NULL;
            start = 12;
            end = 100;
            if (chaine != NULL && start < end)
            {
/* (1)*/
               new_ville = malloc (sizeof (*new_ville) * (end - start + 2));
               if (new_ville != NULL)
               {
                  int i;
/* (2) */
                  for (i = start; i <= end; i++)
                  {
/* (3) */
                     new_ville[i-start] = chaine[i];
                  }
                  new_ville[i-start] = '\0';
               }
               else
               {
                  fprintf (stderr, "Memoire insuffisante\n");
                  exit (EXIT_FAILURE);
               }
            }
/* CP */
            new_cp = NULL;
            start = 7;
            end = 10;
            if (chaine != NULL && start < end)
            {
/* (1)*/
               new_cp = malloc (sizeof (*new_cp) * (end - start + 2));
               if (new_cp != NULL)
               {
                  int i;
/* (2) */
                  for (i = start; i <= end; i++)
                  {
/* (3) */
                     new_cp[i-start] = chaine[i];
                  }
                  new_cp[i-start] = '\0';
               }
               else
               {
                  fprintf (stderr, "Memoire insuffisante\n");
                  exit (EXIT_FAILURE);
               }
            }
//*            printf("CP : %s\n", new_cp);
/* JJNAISSANCE */
            new_jj = NULL;
            start = 7;
            end = 8;
            if (chaine != NULL && start < end)
            {
/* (1)*/
               new_jj = malloc (sizeof (*new_jj) * (end - start + 2));
               if (new_jj != NULL)
               {
                  int i;
/* (2) */
                  for (i = start; i <= end; i++)
                  {
/* (3) */
                     new_jj[i-start] = chaine[i];
                  }
                  new_jj[i-start] = '\0';
               }
               else
               {
                  fprintf (stderr, "Memoire insuffisante\n");
                  exit (EXIT_FAILURE);
               }
            }
//*            printf("JJ : %s\n", new_jj);
/* MMNAISSANCE */
            new_mm = NULL;
            start = 9;
            end = 10;
            if (chaine != NULL && start < end)
            {
/* (1)*/
               new_mm = malloc (sizeof (*new_mm) * (end - start + 2));
               if (new_mm != NULL)
               {
                  int i;
/* (2) */
                  for (i = start; i <= end; i++)
                  {
/* (3) */
                     new_mm[i-start] = chaine[i];
                  }
                  new_mm[i-start] = '\0';
               }
               else
               {
                  fprintf (stderr, "Memoire insuffisante\n");
                  exit (EXIT_FAILURE);
               }
            }
//*            printf("MM : %s\n", new_mm);
/* AANAISSANCE */
            new_aa = NULL;
            start = 11;
            end = 14;
            if (chaine != NULL && start < end)
            {
/* (1)*/
               new_aa = malloc (sizeof (*new_aa) * (end - start + 2));
               if (new_aa != NULL)
               {
                  int i;
/* (2) */
                  for (i = start; i <= end; i++)
                  {
/* (3) */
                     new_aa[i-start] = chaine[i];
                  }
                  new_aa[i-start] = '\0';
               }
               else
               {
                  fprintf (stderr, "Memoire insuffisante\n");
                  exit (EXIT_FAILURE);
               }
            }
//*            printf("AA : %s\n", new_aa);
/* 3000 */
            ptr3000 = "";
            ptr3000 = strstr(str1, str3000);
            if (ptr3000 != NULL)
            {
               strcpy(num, new_s);
            }
/* 3101 */
            ptr3101 = "";
            ptr3101 = strstr(str1, str3101);
            if (ptr3101 != NULL)
            {
               strcpy(nom, new_s);
            }
/* 3102 */
            ptr3102 = "";
            ptr3102 = strstr(str1, str3102);
            if (ptr3102 != NULL)
            {
               strcpy(prenom, new_s);
            }
/* 3103 */
            ptr3103 = "";
            ptr3103 = strstr(str1, str3103);
            if (ptr3103 != NULL)
            {
//*               strcpy(naissance, new_s);
//*               printf("%s\n", naissance);
//*               naissance = "";
               strcat(naissance, new_aa);
               strcat(naissance, new_mm);
               strcat(naissance, new_jj);
//*               printf("Naissance : %s\n", naissance);
            }
/* 3106 */
            ptr3106 = "";
            ptr3106 = strstr(str1, str3106);
            if (ptr3106 != NULL)
            {
               strcpy(cp, new_cp);
               strcpy(ville, new_ville);
            }
/* 3107 */
            ptr3107 = "";
            ptr3107 = strstr(str1, str3107);
            if (ptr3107 != NULL)
            {
               strcpy(adresse, new_s);
            }
/* 3110 */
            ptr3110 = "";
            ptr3110 = strstr(str1, str3110);
            if (ptr3110 != NULL)
            {
               strcpy(sexenum, new_s);
               v = strncmp(sexenum, sexefille, 1);
               if (v == 0)
               {
                  strcpy(sexe, "F");
               }
               v = strncmp(sexenum, sexegarcon, 1);
               if (v == 0)
               {
                  strcpy(sexe, "H");
               }
             }
        }
        printf("Num : %s", num);
        printf("Nom : %s", nom);
        printf("Prénom : %s", prenom);
        printf("Naissance : %s\n", naissance);
        printf("CP : %s\n", cp);
        printf("Ville : %s", ville);
        printf("Adresse : %s", adresse);
        printf("Sexe : %s\n\n", sexe);
/* Fermeture du fichier */
        fclose(fichier);

/* URL */
        strcat(Explorer, Explorer1);
        strcat(Explorer, action);
        strcat(Explorer, ExplorerAdd);
        strcat(Explorer, user);
        strcat(Explorer, ExplorerAdd);
        strcat(Explorer, code_labo);
        strcat(Explorer, ExplorerAdd);
        strcat(Explorer, id_med);
        strcat(Explorer, ExplorerAdd);
        strcat(Explorer, ExplorerNum);
        strcat(Explorer, num);
        strcat(Explorer, ExplorerAdd);
        strcat(Explorer, ExplorerNom);
        strcat(Explorer, nom);
        strcat(Explorer, ExplorerAdd);
        strcat(Explorer, ExplorerPreNom);
        strcat(Explorer, prenom);
        strcat(Explorer, ExplorerAdd);
        strcat(Explorer, ExplorerSexe);
        strcat(Explorer, sexe);
        strcat(Explorer, ExplorerAdd);
        strcat(Explorer, ExplorerNaissance);
        strcat(Explorer, naissance);
        strcat(Explorer, ExplorerAdd);
        strcat(Explorer, ExplorerAdresse);
        strcat(Explorer, adresse);
        strcat(Explorer, ExplorerAdd);
        strcat(Explorer, ExplorerCP);
        strcat(Explorer, cp);
        strcat(Explorer, ExplorerAdd);
        strcat(Explorer, ExplorerVille);
        strcat(Explorer, ville);
        strcat(Explorer, ExplorerAdd);
        strcat(Explorer, ExplorerPays);
        strcat(Explorer, pays);
        strcat(Explorer, ExplorerAdd);
        strcat(Explorer, ExplorerTel);
        strcat(Explorer, tel);
        strcat(Explorer, ExplorerAdd);
        strcat(Explorer, ExplorerMobile);
        strcat(Explorer, mobile);
        strcat(Explorer, ExplorerAdd);
        strcat(Explorer, ExplorerEmail);
        strcat(Explorer, email);
        strcat(Explorer, ExplorerAdd);
        strcat(Explorer, open);
        printf("Explorer A : %s\n", Explorer);


//*        Process.Start(Explorer)
    }

    return 0;
}


Le prénom devrait Hélène et il apparaît Hùlpne
Comment est-ce que je peux résoudre ce problème ?

Merci et bonne journée

A bientôt
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_perles Messages postés 74 Date d'inscription samedi 7 juillet 2007 Statut Membre Dernière intervention 29 mars 2015
27 mars 2015 à 00:40
Bonjour,

Voici mon code ci-dessous :

#include <stdlib.h>
#include <stdio.h>
#define TAILLE_MAX 1000

int main(int argc, char *argv[])
{
    FILE* fichier = NULL;

    int compteur;
    int v;

    char Expl[TAILLE_MAX] = "start http://www.google.ch";
    char Explor[TAILLE_MAX] =
    "start https://www.proxilis.ch/directlab/link_doc.php?action=ADDPAT&user=125784b92703c&code_labo=0&id_med=9997&num=987654&nom=NAIMARRE&prenom=Jean&sexe=H&naissance=19101010&adresse=Route de Satigny,42&cp=1217&ville=Meyrin&pays=Suisse&tel=0223413314&mobile=0792857520&email=info@prolis.ch&open=1";


        printf("Explor : %s\n", Explor);

//*        system("start http://www.google.ch");
        system(Explor);
//*        Process.Start(Explorer)

    return 0;
}


Je souhaiterais que on ouvre l'URL dans Explorer

https://www.proxilis.ch/directlab/link_doc.php?action=ADDPAT&user=125784b92703c&code_labo=0&id_med=9997&num=987654&nom=NAIMARRE&prenom=Jean&sexe=H&naissance=19101010&adresse=Route de Satigny,42&cp=1217&ville=Meyrin&pays=Suisse&tel=0223413314&mobile=0792857520&email=info@prolis.ch&open=1

Le problème est que dans Explorer mon URL est tronquée

https://www.proxilis.ch/directlab/link_doc.php?action=ADDPAT

Pouvez-vous m'aider ?

Merci

A bientôt
0
Whismeril Messages postés 19022 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 17 avril 2024 656
27 mars 2015 à 07:38
Bonjour cptpingu t'a dit d'aller apprendre les bases.
Reviens donc quand ce sera fait.
http://franckh.developpez.com/articles/c-ansi/bien-debuter-en-c/
0
Rejoignez-nous