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 :

probleme de création de fichiers avec une boucle


Sujet :

C

  1. #1
    Membre à l'essai
    Inscrit en
    Octobre 2008
    Messages
    11
    Détails du profil
    Informations forums :
    Inscription : Octobre 2008
    Messages : 11
    Points : 12
    Points
    12
    Par défaut probleme de création de fichiers avec une boucle
    Salut à tous

    je souhaite modifier mon programmeen y intégrant une boucle qui permettent de créer un nouveau fichier a chaque réitération de la boucle. Mon essai n'est pas concluant car il n'y a qu'un seul fichier créé, le premier.

    Voici le bout de code intéressant:

    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
    FILE *data;
     
     
    int main(int argc, char **argv)
    {
    	int	ttt, j;
    	char	fname[20],fnamebis[20];
    	FILE	*fopen(const char *, const char *), *fileid;
     
    	for (par=0;par<10;par++) /*nombre de modif du parametre*/
    	{
    		deathrate = DEATHRATE+0.1*par;
    		InitStuff();
     
    		sprintf(fnamebis,"data_sans_empty_space_deathrate=%-1f.txt",POLLDIST);
    		data=fopen(fnamebis,"w");
     
    		}
    	}
    	fclose(data);
     
    	return (0);
    }
    }
    est ce que quelqu'un aurait une idée ?
    merci beaucoup !

  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
    Citation Envoyé par bobo696 Voir le message
    Salut à tous

    je souhaite modifier mon programmeen y intégrant une boucle qui permettent de créer un nouveau fichier a chaque réitération de la boucle. Mon essai n'est pas concluant car il n'y a qu'un seul fichier créé, le premier.

    Voici le bout de code intéressant:
    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
    34
     
     
    -------------- Build: Debug in hello ---------------
     
    Compiling: main.c
    Linking console executable: bin\Debug\hello.exe
    C:\dev\hello\main.c:1: error: syntax error before '*' token
    C:\dev\hello\main.c:1: warning: type defaults to `int' in declaration of `data'
    C:\dev\hello\main.c:1: error: ISO C forbids data definition with no type or storage class
    C:\dev\hello\main.c: In function `main':
    C:\dev\hello\main.c:8: error: `FILE' undeclared (first use in this function)
    C:\dev\hello\main.c:8: error: (Each undeclared identifier is reported only once
    C:\dev\hello\main.c:8: error: for each function it appears in.)
    C:\dev\hello\main.c:8: warning: implicit declaration of function `fopen'
    C:\dev\hello\main.c:8: error: syntax error before "const"
    C:\dev\hello\main.c:10: error: `par' undeclared (first use in this function)
    C:\dev\hello\main.c:12: error: `deathrate' undeclared (first use in this function)
    C:\dev\hello\main.c:12: error: `DEATHRATE' undeclared (first use in this function)
    C:\dev\hello\main.c:13: warning: implicit declaration of function `InitStuff'
    C:\dev\hello\main.c:15: warning: implicit declaration of function `sprintf'
    C:\dev\hello\main.c:15: error: `POLLDIST' undeclared (first use in this function)
    C:\dev\hello\main.c:16: warning: assignment makes pointer from integer without a cast
    C:\dev\hello\main.c:6: warning: unused variable `ttt'
    C:\dev\hello\main.c:6: warning: unused variable `j'
    C:\dev\hello\main.c:7: warning: unused variable `fname'
    C:\dev\hello\main.c: At top level:
    C:\dev\hello\main.c:4: warning: unused parameter 'argc'
    C:\dev\hello\main.c:4: warning: unused parameter 'argv'
    C:\dev\hello\main.c:20: warning: type defaults to `int' in declaration of `fclose'
    C:\dev\hello\main.c:20: warning: parameter names (without types) in function declaration
    C:\dev\hello\main.c:20: error: ISO C forbids data definition with no type or storage class
    C:\dev\hello\main.c:22: error: syntax error before "return"
    Process terminated with status 1 (0 minutes, 0 seconds)
    12 errors, 12 warnings
    Pas de Wi-Fi à la maison : CPL

  3. #3
    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
    Citation Envoyé par Emmanuel Delahaye Voir le message
    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
    34
     
     
    -------------- Build: Debug in hello ---------------
     
    Compiling: main.c
    Linking console executable: bin\Debug\hello.exe
    C:\dev\hello\main.c:1: error: syntax error before '*' token
    C:\dev\hello\main.c:1: warning: type defaults to `int' in declaration of `data'
    C:\dev\hello\main.c:1: error: ISO C forbids data definition with no type or storage class
    C:\dev\hello\main.c: In function `main':
    C:\dev\hello\main.c:8: error: `FILE' undeclared (first use in this function)
    C:\dev\hello\main.c:8: error: (Each undeclared identifier is reported only once
    C:\dev\hello\main.c:8: error: for each function it appears in.)
    C:\dev\hello\main.c:8: warning: implicit declaration of function `fopen'
    C:\dev\hello\main.c:8: error: syntax error before "const"
    C:\dev\hello\main.c:10: error: `par' undeclared (first use in this function)
    C:\dev\hello\main.c:12: error: `deathrate' undeclared (first use in this function)
    C:\dev\hello\main.c:12: error: `DEATHRATE' undeclared (first use in this function)
    C:\dev\hello\main.c:13: warning: implicit declaration of function `InitStuff'
    C:\dev\hello\main.c:15: warning: implicit declaration of function `sprintf'
    C:\dev\hello\main.c:15: error: `POLLDIST' undeclared (first use in this function)
    C:\dev\hello\main.c:16: warning: assignment makes pointer from integer without a cast
    C:\dev\hello\main.c:6: warning: unused variable `ttt'
    C:\dev\hello\main.c:6: warning: unused variable `j'
    C:\dev\hello\main.c:7: warning: unused variable `fname'
    C:\dev\hello\main.c: At top level:
    C:\dev\hello\main.c:4: warning: unused parameter 'argc'
    C:\dev\hello\main.c:4: warning: unused parameter 'argv'
    C:\dev\hello\main.c:20: warning: type defaults to `int' in declaration of `fclose'
    C:\dev\hello\main.c:20: warning: parameter names (without types) in function declaration
    C:\dev\hello\main.c:20: error: ISO C forbids data definition with no type or storage class
    C:\dev\hello\main.c:22: error: syntax error before "return"
    Process terminated with status 1 (0 minutes, 0 seconds)
    12 errors, 12 warnings
    Ceci :
    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
     
    #include <stdio.h>
     
    #define DEATHRATE .2
     
    int main (void)
    {
       int par;
       for (par = 0; par < 10; par++) /*nombre de modif du parametre */
       {
          double deathrate = DEATHRATE + 0.1 * par;
          char fnamebis[128];
          sprintf (fnamebis, "data_sans_empty_space_deathrate_%g.txt", deathrate);
          {
             FILE *fp = fopen (fnamebis, "w");
             if (fp != NULL)
             {
                fclose (fp);
             }
          }
       }
     
       return 0;
    }
    produit :
    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
     
    <...>
    C:\dev\hello>dir d*
     Le volume dans le lecteur C s'appelle SW_Preload
     Le numéro de série du volume est AA6B-BA4D
     
     Répertoire de C:\dev\hello
     
    15/12/2008  00:54                87 data.txt
    19/01/2009  14:01                 0 data_sans_empty_space_deathrate_0.2.txt
    19/01/2009  14:01                 0 data_sans_empty_space_deathrate_0.3.txt
    19/01/2009  14:01                 0 data_sans_empty_space_deathrate_0.4.txt
    19/01/2009  14:01                 0 data_sans_empty_space_deathrate_0.5.txt
    19/01/2009  14:01                 0 data_sans_empty_space_deathrate_0.6.txt
    19/01/2009  14:01                 0 data_sans_empty_space_deathrate_0.7.txt
    19/01/2009  14:01                 0 data_sans_empty_space_deathrate_0.8.txt
    19/01/2009  14:01                 0 data_sans_empty_space_deathrate_0.9.txt
    19/01/2009  14:01                 0 data_sans_empty_space_deathrate_1.1.txt
    19/01/2009  14:01                 0 data_sans_empty_space_deathrate_1.txt
    18/01/2009  12:46             1*036 dico.c
    18/01/2009  12:47               119 dico.h
    26/11/2008  09:40                57 dico.txt
    11/01/2009  14:29                 4 donnees.txt
                  17 fichier(s)            2*461 octets
                   0 Rép(s)  18*153*365*504 octets libres
     
    C:\dev\hello>
    Pas de Wi-Fi à la maison : CPL

  4. #4
    Membre à l'essai
    Inscrit en
    Octobre 2008
    Messages
    11
    Détails du profil
    Informations forums :
    Inscription : Octobre 2008
    Messages : 11
    Points : 12
    Points
    12
    Par défaut
    merci pour votre réponse, malheureusement je crois que je ne peux pas écrire mon code ainsi : le FILE *fp que vous introduisez est à l'intérieur de la fonction principale. Je l'ai déclaré en variable globale car je réutilise ce fichier de données dans une sous fonction (avec fprintf)

    Voici le code entier si cela est utile... :

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Toutes versions] Création du macro avec une boucle
    Par Kiera dans le forum Macros et VBA Excel
    Réponses: 9
    Dernier message: 09/01/2015, 11h26
  2. Création de dossiers avec une boucle
    Par LUCAS31 dans le forum VBScript
    Réponses: 2
    Dernier message: 27/08/2012, 17h53
  3. Traiter 100 fichiers avec une boucle sous VBA excel
    Par sebastien06 dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 02/04/2008, 09h39
  4. probleme de création de variable dans une boucle
    Par misterweb dans le forum Langage
    Réponses: 2
    Dernier message: 16/01/2007, 19h21
  5. Comment parcourir un fichier texte avec une boucle ?
    Par kikica dans le forum Langage
    Réponses: 2
    Dernier message: 15/11/2005, 17h13

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