File: Stare vježbe/vjezbe12/74a__file_copy.c

  1. /*
  2.   74a__file_copy.c
  3.   Naredba fscanf.
  4.   -----
  5.   Program kopira sadrzaj jedne datoteke u drugu. Imena datoteka ucitavaju
  6.   se iz programa.
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <ctype.h>
  12.  
  13. int main ( void )
  14. {
  15. FILE *source, *destination;
  16. char src_name[80], dest_name[80], ch;
  17.  
  18. printf ("Unesite ime polazne datoteke: ");
  19. scanf ("%s", src_name);
  20.  
  21. printf ("Unesite ime ciljne datoteke: ");
  22. scanf ("%s", dest_name);
  23.  
  24. if ((source=fopen(src_name, "rt")) == NULL)
  25. {
  26. printf ("Ne mogu otvoriti datoteku %s za citanje.\n", src_name);
  27. exit(1);
  28. }
  29.  
  30. if ((destination=fopen(dest_name, "rt")) != NULL)
  31. {
  32. printf ("Datoteka %s postoji. Prebrisati (y/n)? ", dest_name);
  33. /* U ulaznom bufferu (stdin) ostao je jedan '\n', pa ga
  34.   moramo rucno ucitati */
  35. scanf("%*c");
  36. if ((ch=toupper(getchar())) != 'Y')
  37. fputc(ch, stdout), exit(1);
  38. }
  39.  
  40. destination=fopen(dest_name, "wt");
  41.  
  42. while (!feof(source)) {
  43. fscanf (source, "%c", &ch);
  44. fprintf (destination, "%c", ch);
  45. }
  46.  
  47. /* Ovu petlju moguce je zapisati i ovako: */
  48. /*
  49. while (fscanf (source, "%c", &ch) > 0)
  50.   fprintf (destination, "%c", ch);
  51. */
  52. /* funkcija fscanf() vraca broj uspjesno ucitanih/pridruzenih polja
  53. specificiranih u formatirajucem stringu */
  54.  
  55. fclose (source);
  56. fclose (destination);
  57.  
  58. return 0;
  59. }
  60.