Bien programmer en langage C
Vous souhaitez réagir à ce message ? Créez un compte en quelques clics ou connectez-vous pour continuer.
Le Deal du moment : -47%
DOMO DO1032SV – Aspirateur balai 2 en 1
Voir le deal
49.99 €

aide avec fopen sur code::block 8.02

2 participants

Aller en bas

aide avec fopen sur code::block 8.02 Empty aide avec fopen sur code::block 8.02

Message  don1design Mer 21 Juil 2010 - 13:50

Bonjour, tout le monde
moi j utilise code blocks pour compiler mes programmes en c

j'ai besoin de rajouter une variable
date
qui contient la date du jour au nom d'un fichier.txt
qui devra être
transaction_date.txt

j'ai utilisé différentes méthodes
mais je n arrive pas a créer le fichier
voici les méthodes que j'ai utilisé


méthode 1

Code:

char dateEtHeureDuJour();
char date[11], Heure[256];
int main(int argc, char *argv[])
{
dateEtHeureDuJour();

FILE* fichier = NULL;

char tmp[500];
sprintf(tmp, "transaction_%s.txt", date);
printf("\n%s", tmp);
fichier = fopen(tmp, "w");
fclose(fichier);
return 0;
}



methode 2
Code:


char dateEtHeureDuJour();
char date[11], Heure[256];
int main()

{
dateEtHeureDuJour();
char nom1[27]="transaction_";
char nom3[]=".txt";
char nom2[15]="";
char* nomF;
strcat(nom2, date);
strcat(nom2, nom3);
strcat(nom1, nom2);
printf("\n%s", nom1); /******L'affichage marche tres bien*******/
nomF = (char*)malloc(sizeof(char));
nomF=&nom1[0];
printf("\n%s", nomF); /******L'affichage marche tres bien*******/
FILE* fichier = NULL;
fichier = fopen(nomF, "w");
fclose(fichier);

return 0;
}


j'aimerais avoir une méthode universelle qui fonctionne sur tout les compilateurs car ces deux méthodes n'aboutissent à rien sur code::blocks

don1design

Messages : 6
Date d'inscription : 02/07/2010

Revenir en haut Aller en bas

aide avec fopen sur code::block 8.02 Empty Re: aide avec fopen sur code::block 8.02

Message  -ed- Dim 25 Juil 2010 - 10:31

C'est pas un problème de compilateur. Tes codes sont incomplets :
Code:


-------------- Build: Debug in hello ---------------

Compiling: main.c
Linking console executable: bin\Debug\hello.exe
C:\dev\hello\main.c:2: warning: function declaration isn't a prototype
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: error: 'fichier' undeclared (first use in this function)
C:\dev\hello\main.c:8: error: 'NULL' undeclared (first use in this function)
C:\dev\hello\main.c:10: warning: ISO C90 forbids mixed declarations and code
C:\dev\hello\main.c:11: warning: implicit declaration of function 'sprintf'
C:\dev\hello\main.c:11: warning: incompatible implicit declaration of built-in function 'sprintf'
C:\dev\hello\main.c:12: warning: implicit declaration of function 'printf'
C:\dev\hello\main.c:12: warning: incompatible implicit declaration of built-in function 'printf'
C:\dev\hello\main.c:13: warning: implicit declaration of function 'fopen'
C:\dev\hello\main.c:14: warning: implicit declaration of function 'fclose'
C:\dev\hello\main.c:4: warning: unused parameter 'argc'
C:\dev\hello\main.c:4: warning: unused parameter 'argv'
Process terminated with status 1 (0 minutes, 0 seconds)
5 errors, 10 warnings

Code:


-------------- Build: Debug in hello ---------------

Compiling: main.c
Linking console executable: bin\Debug\hello.exe
C:\dev\hello\main.c:3: warning: function declaration isn't a prototype
C:\dev\hello\main.c:7: warning: function declaration isn't a prototype
C:\dev\hello\main.c: In function 'main':
C:\dev\hello\main.c:9: warning: ISO C90 forbids mixed declarations and code
C:\dev\hello\main.c:13: warning: implicit declaration of function 'strcat'
C:\dev\hello\main.c:13: warning: incompatible implicit declaration of built-in function 'strcat'
C:\dev\hello\main.c:16: warning: implicit declaration of function 'printf'
C:\dev\hello\main.c:16: warning: incompatible implicit declaration of built-in function 'printf'
C:\dev\hello\main.c:17: warning: implicit declaration of function 'malloc'
C:\dev\hello\main.c:17: warning: incompatible implicit declaration of built-in function 'malloc'
C:\dev\hello\main.c:20: error: 'FILE' undeclared (first use in this function)
C:\dev\hello\main.c:20: error: (Each undeclared identifier is reported only once
C:\dev\hello\main.c:20: error: for each function it appears in.)
C:\dev\hello\main.c:20: error: 'fichier' undeclared (first use in this function)
C:\dev\hello\main.c:20: error: 'NULL' undeclared (first use in this function)
C:\dev\hello\main.c:21: warning: implicit declaration of function 'fopen'
C:\dev\hello\main.c:22: warning: implicit declaration of function 'fclose'
Process terminated with status 1 (0 minutes, 0 seconds)
5 errors, 11 warnings
-ed-
-ed-
Admin
Admin

Messages : 290
Date d'inscription : 26/05/2008
Age : 67
Localisation : Paris 14eme arrondissement (75, France)

http://bien-programmer.fr

Revenir en haut Aller en bas

aide avec fopen sur code::block 8.02 Empty Re: aide avec fopen sur code::block 8.02

Message  -ed- Dim 25 Juil 2010 - 10:48

Ton premier code est presque correct. Je propose donc de commencer par écrire un code de test simple qui valide le principe (sous Windows).
Code:

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

static void get_date (char *s)
{
  strcpy (s, "2010_07_25");    /* pour tester le principe */
}

int main (void)
{
  char date[32];

  get_date (date);

  {
      char tmp[500];
      sprintf (tmp, "transaction_%s.txt", date);
      printf ("\n%s", tmp);

      {

        FILE *fichier = fopen (tmp, "w");
        if (fichier != NULL)
        {
            fclose (fichier);
            system ("dir tra*");
            printf ("OK\n");
        }
        else
        {
            printf ("err\n");
        }

      }
  }
  return 0;
}
ce qui donne :
Code:


transaction_2010_07_25.txt 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

25/07/2010  09:45                0 transaction_2010_07_25.txt
              1 fichier(s)                0 octets
              0 Rép(s)  11 478 990 848 octets libres
OK

Process returned 0 (0x0)  execution time : 0.028 s
Press any key to continue.
Pose des questions si tu ne comprends pas.
-ed-
-ed-
Admin
Admin

Messages : 290
Date d'inscription : 26/05/2008
Age : 67
Localisation : Paris 14eme arrondissement (75, France)

http://bien-programmer.fr

Revenir en haut Aller en bas

aide avec fopen sur code::block 8.02 Empty Re: aide avec fopen sur code::block 8.02

Message  Contenu sponsorisé


Contenu sponsorisé


Revenir en haut Aller en bas

Revenir en haut

- Sujets similaires

 
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum
Ne ratez plus aucun deal !
Abonnez-vous pour recevoir par notification une sélection des meilleurs deals chaque jour.
IgnorerAutoriser