1. 程式人生 > >C語言利用fgetc復制拷貝文件內容

C語言利用fgetc復制拷貝文件內容

c語言 print 關閉 printf pen 利用 文件內容 urn clas

#include <stdio.h>
#include <stdlib.h>
//文件的內容復制

int main(int a,char *argv[]){
	if(a!=3){
		printf("useage:%s source!\n", argv[0]);
		exit(1);
	}
	FILE *fp1, *fp2;
	fp1 = fopen(argv[1],"r");
	if(fp1==NULL){
	     printf("source file open error");
	     exit(1);
	}
	fp2 = fopen(argv[2],"w");
	if(fp2==NULL){
	     printf("target file open error");
		exit(1);	
	}
	
	int ch;
	while((ch=fgetc(fp1)) != EOF){
		fputc(ch, fp2);
	}
	//關閉流
	if(fclose(fp1)!=0){
		printf("source file close error");
	}else if(fclose(fp2)!=0){
		printf("target file close error");
	}
	
	return 0;

}

  

C語言利用fgetc復制拷貝文件內容