File: Stare vježbe/vjezbe12/74a__file_copy.c
/* 74a__file_copy.c Naredba fscanf. ----- Program kopira sadrzaj jedne datoteke u drugu. Imena datoteka ucitavaju se iz programa. */ #include <stdio.h> #include <stdlib.h> #include <ctype.h> int main ( void ) { FILE *source, *destination; char src_name[80], dest_name[80], ch; printf ("Unesite ime polazne datoteke: "); scanf ("%s", src_name); printf ("Unesite ime ciljne datoteke: "); scanf ("%s", dest_name); if ((source=fopen(src_name, "rt")) == NULL) { printf ("Ne mogu otvoriti datoteku %s za citanje.\n", src_name ); exit(1); } if ((destination=fopen(dest_name, "rt")) != NULL) { printf ("Datoteka %s postoji. Prebrisati (y/n)? ", dest_name ); /* U ulaznom bufferu (stdin) ostao je jedan '\n', pa ga moramo rucno ucitati */ scanf("%*c"); if ((ch=toupper(getchar())) != 'Y') fputc(ch, stdout), exit(1); } destination=fopen(dest_name, "wt"); while (!feof(source)) { fscanf (source, "%c", &ch); fprintf (destination, "%c", ch); } /* Ovu petlju moguce je zapisati i ovako: */ /* while (fscanf (source, "%c", &ch) > 0) fprintf (destination, "%c", ch); */ /* funkcija fscanf() vraca broj uspjesno ucitanih/pridruzenih polja specificiranih u formatirajucem stringu */ fclose (source); fclose (destination); return 0; }
|