Commande "cp" sous Unix (version Trivial)

From Tuxunix
Jump to: navigation, search

Exemple


$ copier

Usage: ./copier fichierSource fichierDestination

$ cat test.txt

test ma commande copier

$ copier test.txt txt.txt

$ cat txt.txt

test ma commande copier


Code


   1./*
   2. * copier.c for (self)
   3. *
   4. * Made by tuxedo
   5. *
   6. * Started on lun 26 nov 2007 12:49:47 pterrier
   7. * Last update lun 26 nov 2007 12:59:46 pterrier
   8.*/
   9.#include <stdio.h>
  10.#include <stdlib.h>
  11.#define MAXSIZE 100
  12.      
  13./*readFile*/
  14.void readFile(const char *fileSrc, const char *typeOpenSrc, const char *fileDst, const char *typeOpenDst){
  16.    char character[MAXSIZE]="";
  17.    FILE *ficSrc= NULL;
  18.    FILE *ficDst= NULL;
  19.     
  20.    ficSrc=fopen(fileSrc, typeOpenSrc);
  21.    ficDst=fopen(fileDst, typeOpenDst);
  22.    if(ficSrc != NULL){
  23.      while(fgets(character, MAXSIZE, ficSrc) != NULL && !feof(ficSrc)){
  24.          if(ficDst != NULL){
  25.              fprintf(ficDst, character);
  26.          }else{
  27.              printf("Error write file");
  28.          }
  29.      }
  30.    }else{
  31.      printf("Error read file\n");
  32.    }
  33.    fclose(ficSrc);
  34.    fclose(ficDst);
  35.}/*readFile*/
  36.       
  37.int main(int argc, const char *argv[]){
  38.    argc == 3 ? readFile(argv[1], "r", argv[2], "w") :
  39.    printf("Usage: %s fichierSource fichierDestination\n", argv[0]) ;
  40.    return 0;
  41.}

By TuXedo