IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

lire un fichier en c


Sujet :

C

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2005
    Messages
    102
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2005
    Messages : 102
    Points : 53
    Points
    53
    Par défaut lire un fichier en c
    Bonjour à tous!!
    Je voudrais récupérer des infos dans un fichier texte qui ressemble à ça
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    CAL2 BARC  HHE NANO STS2    2.12206591e-02   0.200   100.00000 2005/02/02 00:00 2037/01/01 00:00
    PAZ2 01 V  1.50000000e-06                 1   2
     -5.20000000e-02  0.00000000e+00
      0.00000000e+00  0.00000000e+00
      0.00000000e+00  0.00000000e+00
    DIG2 02  1.00000000e+06   100.00000
    CAL2 BARC  HHN NANO STS2    2.12206591e-02   0.200   100.00000 2005/02/02 00:00 2037/01/01 00:00
    PAZ2 01 V  1.50000000e-06                 1   2
     -5.20000000e-02  0.00000000e+00
      0.00000000e+00  0.00000000e+00
      0.00000000e+00  0.00000000e+00
    DIG2 02  1.00000000e+06   100.00000
    CAL2 BARC  HHZ NANO STS2    2.12206591e-02   0.200   100.00000 2005/02/02 00:00 2037/01/01 00:00
    PAZ2 01 V  1.50000000e-06                 1   2
     -5.20000000e-02  0.00000000e+00
      0.00000000e+00  0.00000000e+00
      0.00000000e+00  0.00000000e+00
    DIG2 02  1.00000000e+06   100.00000
    En fait je veux récuperer les infos des lignes commencants par CAL2, et je dois tester aussi si ma variable appellée g2header.Stn correspond bien à BARC, situé après le CAL2.

    J'ai commencé à écrire qqch mais je dois dire que je reste un peu bloqué:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    fpp = fopen(calib_file,"r");
    		fgets(line,256,fpp);
            while ( feof(fpp) == 0 ) {
    			if ( line[0] != '#' ) {
    				if ( strlen(line) > 2 ) {
    					if ( strncmp(line,"CAL2 ",5) == 0 ) {
    						scanf(line,"%*s %s %s %s %s %d %d",&station_name,&station_chan,&station_aux,&station_sensor,&calib,&calper);
    						if ( strcmp(station_name,g2head.Stn)==0){
    							if ( strcmp(station_chan[2],g2head.Chn[2])==0){
    								strcpy(g2head->Chn,station_chan);
    								strcpy(g2head->Aux,station_aux);
    								g2head->CalibFact=calib;
    								g2head->CalibPer=calper;
    								strcpy(g2head->SensorType,station_sensor);
    								if ( strcmp(station_chan,"HHE")==0 ){
    								g2head.Hang=90.0;
    								g2head.Vang=90.0;
    								}
    								if ( strcmp(station_chan,"HHN")==0 ){
    								g2head.Hang=90.0;
    								g2head.Vang=0.0;
    								}
    								if ( strcmp(station_chan,"HHZ")==0 ){
    								g2head.Hang=-1.0;
    								g2head.Vang=-1.0;
    								}
    							}	
    						}
    					}
    				}
    			}
    			fgets(line,256,fpp);
    		}
    	  fclose(fpp);
    Merci d'avance pour votre aide!!

    peuf23

  2. #2
    Expert éminent sénior
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Points : 20 985
    Points
    20 985
    Par défaut Re: lire un fichier en c
    Citation Envoyé par peuf23
    J'ai commencé à écrire qqch mais je dois dire que je reste un peu bloqué:
    Ben oui, ce code ne compile pas car il est incomplet.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
     
    ../main.c:16: warning: type defaults to `int' in declaration of `fpp'
    ../main.c:16: error: implicit declaration of function `fopen'
    ../main.c:16: error: `calib_file' undeclared here (not in a function)
    ../main.c:16: error: initializer element is not constant
     
    ../main.c:16: warning: data definition has no type or storage class
     
    ../main.c:17: error: syntax error before numeric constant
    ../main.c:17: warning: type defaults to `int' in declaration of `fgets'
    ../main.c:17: warning: function declaration isn't a prototype
    ../main.c:17: warning: data definition has no type or storage class
    ../main.c:26: error: syntax error before '->' token
    ../main.c:26: warning: type defaults to `int' in declaration of `strcpy'
    ../main.c:26: warning: function declaration isn't a prototype
    ../main.c:26: warning: conflicting types for built-in function 'strcpy'
    ../main.c:26: warning: data definition has no type or storage class
    ../main.c:27: error: syntax error before '->' token
    ../main.c:29: error: syntax error before '->' token
    ../main.c:29: warning: type defaults to `int' in declaration of `strcpy'
    ../main.c:29: warning: function declaration isn't a prototype
    ../main.c:29: warning: redundant redeclaration of 'strcpy'
    ../main.c:26: warning: previous declaration of 'strcpy' was here
    ../main.c:29: warning: data definition has no type or storage class
    ../main.c:47: error: syntax error before numeric constant
    ../main.c:47: warning: type defaults to `int' in declaration of `fgets'
    ../main.c:47: warning: function declaration isn't a prototype
    ../main.c:47: warning: redundant redeclaration of 'fgets'
    ../main.c:17: warning: previous declaration of 'fgets' was here
    ../main.c:47: warning: data definition has no type or storage class
    ../main.c:49: warning: type defaults to `int' in declaration of `fclose'
    ../main.c:49: warning: parameter names (without types) in function declaration
    ../main.c:49: warning: data definition has no type or storage class
    Merci de poster du code compîlable... et de préciser la nature du 'blocage'. On ne peut pas deviner ce que tu as oublé.
    Un truc comme ça ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
     
    #include <stdio.h>
    #include <string.h>
     
    #define calib_file "data.txt"
     
    static struct
    {
       char const *Stn;
       char Chn[128];
       char Aux[128];
       char SensorType[128];
       double CalibFact;
       double CalibPer;
       double Hang;
       double Vang;
    }
    g2head =
    {
       "CAL2",
          "??R",
    };
     
    int main (void)
    {
       FILE *fpp = fopen (calib_file, "r");
     
       if (fpp != NULL)
       {
          char line[256];
     
          while (fgets (line, sizeof line, fpp) != NULL)
          {
             if (line[0] != '#')
             {
                if (strlen (line) > 2)
                {
                   char station_name[128];
                   char station_chan[128];
                   char station_aux[128];
                   char station_sensor[128];
                   char station_xxx[128];
                   double calib;
                   double calper;
     
                   if (strncmp (line, "CAL2 ", 5) == 0)
                   {
                      int n = sscanf (line, "%s %s %s %s %s %lg %lg"
                                      ,station_name
                                      ,station_chan
                                      ,station_aux
                                      ,station_sensor
                                      ,station_xxx
                                      ,&calib
                                      ,&calper);
     
     
                      if (n == 7)
                      {
                         if (strcmp (station_name, g2head.Stn) == 0)
                         {
                            if (station_chan[2] == g2head.Chn[2])
                            {
                               strcpy (g2head.Chn, station_chan);
                               strcpy (g2head.Aux, station_aux);
                               g2head.CalibFact = calib;
                               g2head.CalibPer = calper;
                               strcpy (g2head.SensorType, station_sensor);
     
                               if (strcmp (station_aux, "HHE") == 0)
                               {
                                  g2head.Hang = 90.0;
                                  g2head.Vang = 90.0;
                               }
                               if (strcmp (station_aux, "HHN") == 0)
                               {
                                  g2head.Hang = 90.0;
                                  g2head.Vang = 0.0;
                               }
                               if (strcmp (station_aux, "HHZ") == 0)
                               {
                                  g2head.Hang = -1.0;
                                  g2head.Vang = -1.0;
                               }
                            }
                         }
     
    #define PRT_S(f) printf ("%-10s = %s\n", #f, g2head.f)
    #define PRT_D(f) printf ("%-10s = %f\n", #f, g2head.f)
     
                         PRT_S (Chn);
                         PRT_S (Aux);
                         PRT_S (SensorType);
                         PRT_D (CalibFact);
                         PRT_D (CalibPer);
                         PRT_D (Hang);
                         PRT_D (Vang);
    #undef PRT_S
    #undef PRT_D
                         puts("");
                      }
                   }
                }
             }
          }
          fclose (fpp);
       }
       else
       {
          perror (calib_file);
       }
       return 0;
    }
    Pas de Wi-Fi à la maison : CPL

  3. #3
    Rédacteur
    Avatar de Franck.H
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2004
    Messages
    6 951
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Service public

    Informations forums :
    Inscription : Janvier 2004
    Messages : 6 951
    Points : 12 462
    Points
    12 462
    Par défaut
    En fait je pense que dans une boucle while de ce genre:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    while (fgets (str, sizeof str, p_file))
    puis un ou plusieurs passage avec strtok style:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    char *p_adr = NULL;
     
    p_adr = strtok (str, " ");  /* Je ne suis pas sur que le caractere de séparation 
                                                peut etre un espace, jamais essayé en fait ! */
    if (strcmp (p_adr, CAL2) == 0)
    {
       p_adr = strtok (NULL, " "); /* Ici on récupère la partie suivante donc dans notre cas: BARC
    }
    Je n'est rien vérifié, à tester mais ca peut etre une idée moins couteuse que ce que tu as fait !
    Mon Site
    Ma bibliothèque de gestion des chaînes de caractères en C

    L'imagination est plus importante que le savoir. A. Einstein

    Je ne répond à aucune question technique par MP, merci d'avance !

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2005
    Messages
    102
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2005
    Messages : 102
    Points : 53
    Points
    53
    Par défaut
    Pardon,

    tu peux trouver tous les fichiers sur http://cecile.maurer.free.fr/y2gse_new/. Pour ma part j'ai modifié la fonction write_gse_intv dans y2gse.c (lignes 870 à 907)...

    Merci pour ton aide

    peuf23

Discussions similaires

  1. [Servlet][EJB]lire un fichier à partir de mon bean
    Par kimausoleil dans le forum Servlets/JSP
    Réponses: 3
    Dernier message: 13/02/2004, 14h26
  2. [MFC] lire un fichier en int
    Par Kevgeii dans le forum MFC
    Réponses: 3
    Dernier message: 23/01/2004, 09h32
  3. Réponses: 5
    Dernier message: 11/01/2004, 20h17
  4. Lire un fichier de 50 gb
    Par Maryves dans le forum MFC
    Réponses: 2
    Dernier message: 08/01/2004, 14h08
  5. Lire le fichier du port série
    Par saibe dans le forum Linux
    Réponses: 7
    Dernier message: 09/04/2003, 09h29

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo