精華區beta NAOE-87 關於我們 聯絡資訊
#include <stdio.h> #include <process.h> #include <string.h> #include <stdlib.h> #include <alloc.h> int main() { FILE *oldfile, *newfile; char oldname[20],newname[20],*buffer,c; int nold,lnew,i,nline,lj,j,index; printf("Input the old filename = ?\n"); scanf("%s",oldname); printf("Input the length of each line for new file L=?\n"); scanf("%d",&lnew); printf("Input the new file name=?\n"); scanf("%s",newname); oldfile=fopen(oldname,"r"); if(oldfile==NULL){ printf("Open old file error\n"); exit(1); } newfile=fopen(newname,"w"); if(newfile==NULL){ printf("Open new file error\n"); exit(2); } fseek(oldfile,0,SEEK_END); nold=ftell(oldfile); fseek(oldfile,0,SEEK_SET); buffer=(char *)calloc(nold,sizeof(char)); if(buffer==NULL){ printf("Memory allocation error\n"); exit(3); } i=0; while((c=fgetc(oldfile))!=EOF){ if(c=='\n')continue; *(buffer+i)=c; ++i; } nline=nold/lnew+1; for(i=1; i<=nline; ++i){ if(i==nline)lj=nold%lnew; else lj=lnew; for(j=0; j<lj; ++j){ index=(i-1)*lnew +j; fputc(buffer[index],newfile); } fprintf(newfile,"\n"); } fclose(oldfile); fclose(newfile); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.twbbs.org) ◆ From: pc86.na.ntu.edu