Blog Address: Http://blog.csdn.net/muyang_ren
1. copy files by character
/*************************************************************************> File name:1_size.c> Author: 梁惠 Chung > Addr: > Created time:2015 April 12 Sunday 22:24 52 seconds ************************************************************** /#include <stdio.h> #include <stdlib.h>int main () { char ch; FILE *FP1,*FP2; FP1 = fopen ("1_size.c", "R"); if (FP1 ==null) { perror ("fopen:"); Exit ( -1); } FP2 = fopen ("1.txt", "w"); if (FP2 ==null) { perror ("fopen:"); Exit ( -1); } while ((Ch=getc (FP1))! = EOF) { putc (CH,FP2); PUTC (ch,stdout); } Fclose (FP1); Fclose (FP2); return 0;}
2. copy files by row
/*************************************************************************> File name:2_line.c> Author: 梁惠 Chung > Addr: > Created time:2015 April 12 Sunday 22:38 22 seconds ************************************************************** /#include <stdio.h> #include <stdlib.h> #define size 40int main () { char str[size]; FILE *FP1,*FP2; FP1 = fopen ("2_line.c", "R"); if (FP1 ==null) { perror ("fopen:"); Exit ( -1); } FP2 = fopen ("2.txt", "w"); if (FP2 ==null) { perror ("fopen:"); Exit ( -1); } while (fgets (STR,SIZE,FP1) = NULL) { fputs (STR,FP2); Fputs (str,stdout); } Fclose (FP1); Fclose (FP2); return 0;}
3. File "Direct io" copy
/*************************************************************************> File name:3_direct.c> Author: Liang > Addr: > Created time:2015 April 12 Sunday 22:47 08 Sec. *********************************************************** /#include <stdio.h> #include <stdlib.h> #include <strings.h> #include <string.h> #define SIZE 100int Main () {char str[size]; FILE *FP1,*FP2; int flend_num; FP1 = fopen ("3_direct.c", "R"); if (FP1 ==null) {perror ("fopen:"); Exit (-1); } FP2 = fopen ("3.txt", "w"); if (FP2 ==null) {perror ("fopen:"); Exit (-1); } fseek (Fp1,0,seek_end); Locate the file pointer to the end of the file Flend_num = Ftell (FP1); Flend_num Store file Total characters fseek (fp1,0,seek_set); Locate the file pointer to the beginning of the file while (Ftell (FP1) < Flend_num) {bzero (str,size); Fread (str,sizeof (str), 1,FP1); To read the file, use sizeof to write full cache space fwrite (Str,strlen (str), 1,FP2); Writes a file using strlen, writes valid characters in the cache to the file fwrite (Str,strlen (str), 1,stdout); } Fclose (FP1); Fclose (FP2); return 0;}
4, file character reverse copy
/*************************************************************************> File name:4_reverse.c> Author: Liang > Addr: > Created time:2015 April 16 Thursday 18:44 55 sec. *********************************************************** /#include <stdio.h> #include <string.h> #include <stdlib.h> #define size 100int main () { FILE *FP1,*FP2; int File_len; Char str[size], ch; printf ("Please enter string:"); scanf ("%s", str); FP1 = fopen ("4_1.txt", "w+r"); Positive sequence File FP2 = fopen ("4_2.txt", "w"); Reverse File fwrite (Str,strlen (str), 1,FP1); The input string is stored in 4_1.txt fseek (FP1, 0, seek_end); Locate the file pointer to the end of the file File_len = Ftell (FP1); for (; file_len>0; file_len--) {fseek (FP1, file_len-1, Seek_set); CH=FGETC (FP1); Get a character from this address FPUTC (CH, FP2); FPUTC (CH, stdout); } putc (' \ n ', stdout); Fclose (FP1); Fclose (FP2); return 0;}
"Standard io" of Pleasant Goat Series