#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
FILE *file=fopen("list","r");//以只讀模式打開文件list
if(file==NULL){
printf("open file failed!!\n");
return 0;
}
fseek(file,0,SEEK_END);//定位文件指針到末尾
int len=ftell(file);//返回當前文件指針與文件頭之間的距離
printf("file length=%d\n",len);
char* ch=(char *)malloc(sizeof(char)*(len+1));//分配文件長度加一大小的內存區
memset(ch,0,len+1);//初始化內存區
rewind(file);//定位文件指針到文件開始位置
fread(ch,1,len,file);//從文件指針當前位置讀len長度的內容到ch內存區
printf("%s\n",ch);//輸出文件內容
fseek(file,0,SEEK_SET);//重置指針
char re[9]={0};
int num=(int)fread(re,1,8,file);//從文件指針當前位置讀8的字節到re中
//寫文件
FILE *wFile=fopen("list2.txt","a+");//以附加方式打開可讀寫的文件
if(wFile==NULL){
printf("Open File Failed!!\n");
return 0;
}
while(num!=0){
fwrite(re,1,num,wFile); //將re中前num個字符寫入wFile文件中
printf("%s",re);
num=(int)fread(re,1,8,file);//從file讀8個字符到re中
}
fclose(file);//關閉已打開的文件
return 0;
}