Programmer en c

info_net_com Messages postés 34 Statut Membre -  
fiddy Messages postés 441 Date d'inscription   Statut Contributeur Dernière intervention   -
Bonjour,

j'ai fais un programme pour copier le contenu d'un fichier dans un autre fichier
voila le code:

#include<stdio.h>
#include<stdlib.h>
void main()
{
char h[10]; int c = 0 ;
printf("hello") ;
FILE* f = NULL;
FILE* fichier = NULL;

f = fopen("d:\\0.txt", "w+") ;
fichier = fopen("d:\\test.txt", "r+") ;

if ( f!= NULL)
{ printf("\n On peut lire et écrire dans le fichier 0.txt\n"); } else {
printf("\nImpossible d'ouvrir le fichier 0.txt\n "); }

if ( fichier!= NULL)
{ printf("\n On peut lire et écrire dans le fichier test.txt\n"); } else {
printf("\nImpossible d'ouvrir le fichier test.txt\n "); }

printf("Entrer votre texte: ");
scanf("%s",h);
fputs(h, fichier);

while ( !(EOF(test.txt));
{
c = fgetc(fichier);
printf("%c", c);
putc(c, f);
} ;

fputs("=====Terminer=======",f);

fclose(fichier) ;
fclose(f) ;

}

mais j'ai probleme en se code
j'ai besoin d'aide svp
me corriger le code ou b1 proposer une autre solution
merci d'avance

2 réponses

  1. fiddy Messages postés 441 Date d'inscription   Statut Contributeur Dernière intervention   1 847
     
    EOF(test.txt)
    EOF n'est pas une fonction. Donc normal que ça ne marche pas.
    Utilise plutôt la fonction feof
    0
  2. jisisv Messages postés 3678 Statut Modérateur 936
     
    Essaye ceci:
     
    johand@osiris:~/src/ccm$ cat affich-20923578-programmer-en-c.c 
    #include <stdio.h> 
    #include <stdlib.h> 
    
    #define SOURCE "source.txt" 
    #define DESTINATION "destination.txt" 
    int main(int argc, char *argv[])
    {
      char h[10];
      int c = 0 ;
      FILE *fildest = NULL, *filorg = NULL;
    
      printf("hello") ;
      fildest = fopen(DESTINATION, "w+") ;
      filorg =  fopen(SOURCE, "r+") ;
    
      if ( NULL != fildest) {
        printf("On peut lire et écrire dans le fichier %s\n", DESTINATION);
      }
      else
        {
          printf("Impossible d'ouvrir le fichier%s\n", DESTINATION);
          return 1;
        }
    
      if ( NULL != filorg ) {
        printf("On peut lire et écrire dans le fichier %s\n", SOURCE);
      }
      else
        {
          printf("Impossible d'ouvrir le fichier %s\n", SOURCE);
          return 1;
        }
    
    
      printf("Entrer votre texte: ");
      scanf("%10s", h);
      fputs(h, fildest);
    
    
      while ( !(feof(filorg))) {
          c = fgetc(filorg);
          fputc((int)c, fildest);
          printf("%c", c);
        } ;
    
        puts("=====Terminer=======");
        fclose(filorg) ;
        fclose(fildest) ;
        return 0;
    }
    
    johand@osiris:~/src/ccm$cat source.txt 
    total 44
    -rw-r--r-- 1 johand johand 2312  2 jan 05:37 19639577.php
    -rw-r--r-- 1 johand johand  856 30 déc 06:16 20337537.c
    -rwxr-xr-x 1 johand johand 6754 19 fév 19:43 20916919-langage-c
    -rw-r--r-- 1 johand johand 1080 19 fév 19:40 20916919-langage-c.c
    -rw-r--r-- 1 johand johand 2959 19 fév 17:54 20916919-langage-c.c~
    -rwxr-xr-x 1 johand johand 5675 20 fév 12:25 affich-20923578-programmer-en-c
    -rw-r--r-- 1 johand johand 1006 20 fév 12:25 affich-20923578-programmer-en-c.c
    -rw-r--r-- 1 johand johand  725 20 fév 11:41 affich-20923578-programmer-en-c.c~
    -rw-r--r-- 1 johand johand 1007 20 fév 12:26 destination.txt
    -rw-r--r-- 1 johand johand    0 20 fév 12:27 source.txt
    johand@osiris:~/src/ccm$ ./affich-20923578-programmer-en-c 
    helloOn peut lire et écrire dans le fichier destination.txt
    On peut lire et écrire dans le fichier source.txt
    Entrer votre texte: 0123456789
    total 44
    -rw-r--r-- 1 johand johand 2312  2 jan 05:37 19639577.php
    -rw-r--r-- 1 johand johand  856 30 déc 06:16 20337537.c
    -rwxr-xr-x 1 johand johand 6754 19 fév 19:43 20916919-langage-c
    -rw-r--r-- 1 johand johand 1080 19 fév 19:40 20916919-langage-c.c
    -rw-r--r-- 1 johand johand 2959 19 fév 17:54 20916919-langage-c.c~
    -rwxr-xr-x 1 johand johand 5675 20 fév 12:25 affich-20923578-programmer-en-c
    -rw-r--r-- 1 johand johand 1006 20 fév 12:25 affich-20923578-programmer-en-c.c
    -rw-r--r-- 1 johand johand  725 20 fév 11:41 affich-20923578-programmer-en-c.c~
    -rw-r--r-- 1 johand johand 1007 20 fév 12:26 destination.txt
    -rw-r--r-- 1 johand johand    0 20 fév 12:27 source.txt
    ?=====Terminer=======
    johand@osiris:~/src/ccm$ cat destination.txt 
    0123456789total 44
    -rw-r--r-- 1 johand johand 2312  2 jan 05:37 19639577.php
    -rw-r--r-- 1 johand johand  856 30 déc 06:16 20337537.c
    -rwxr-xr-x 1 johand johand 6754 19 fév 19:43 20916919-langage-c
    -rw-r--r-- 1 johand johand 1080 19 fév 19:40 20916919-langage-c.c
    -rw-r--r-- 1 johand johand 2959 19 fév 17:54 20916919-langage-c.c~
    -rwxr-xr-x 1 johand johand 5675 20 fév 12:25 affich-20923578-programmer-en-c
    -rw-r--r-- 1 johand johand 1006 20 fév 12:25 affich-20923578-programmer-en-c.c
    -rw-r--r-- 1 johand johand  725 20 fév 11:41 affich-20923578-programmer-en-c.c~
    -rw-r--r-- 1 johand johand 1007 20 fév 12:26 destination.txt
    -rw-r--r-- 1 johand johand    0 20 fév 12:27 source.txt
    


    Johan
    Gates gave ^H sold you the windows.
    GNU gave us the whole house.(Alexandrin)
    0
    1. fiddy Messages postés 441 Date d'inscription   Statut Contributeur Dernière intervention   1 847
       
      Salut,
      Petite erreur que je me permets de signaler :
      scanf("%10s", h);
      Ce n'est pas 10 mais 9, qu'il faut mettre.
      Cdlt,
      0