Sunday, November 8, 2009

Copy files

The following code, copy source file to destination file.
First, It opens source file for "reading" and also opens destination file for writing.If destination file doesn't exist, it will creates it.Then it starts to read from source file line by line ,simultaneously it writes to destination file until reaches to no data or end of source file.

#include
#define TEXT_SIZE 1024+1
int main(void)
{

FILE* fp_in;
FILE* fp_out;
char TEXT[TEXT_SIZE];
int Byte;

fp_in=fopen("test1.txt","r");
if (fp_in==NULL)
{
printf("Error:can not open source file\n");
}
else
{
fp_out=fopen("test3.txt","w");
while(fscanf(fp_in,"%[^\n]\n",TEXT)==1)
{
fprintf(fp_out,"%s\n",TEXT);
}
fclose(fp_in);
fclose(fp_out);
}
printf("done!\n");
return 0;
}

No comments:

Post a Comment