读文件file1.txt的内容到file2.txt

来源:互联网 发布:南京网络问政雨花台区 编辑:程序博客网 时间:2024/05/21 06:36
// file.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <stdio.h>#include <stdlib.h>#define  MAX 10int size;/*读文件file1.txt的内容(例如)123456输出到file2.txt*/int _tmain(int argc, _TCHAR* argv[]){FILE *sorcefp,*dstfp;int *a=(int *)malloc(sizeof(int)*MAX);size=MAX;if (NULL==a){printf("error !\n");exit(-1);}sorcefp=fopen("file1.txt","r");if (NULL==sorcefp){printf("error !\n");exit(-1);}dstfp=fopen("file2.txt","w");if (NULL==dstfp){printf("error !\n");exit(-1);}int i=0,j=0;while(fscanf(sorcefp,"%d",&a[i])!=EOF){i++;j++;if (i>=MAX){int *b=(int *)realloc(a,MAX*sizeof(int)+size);size=size+MAX;if (NULL==b){printf("error !\n");exit(-1);}a=b;}}for (;--j>=0;){fprintf(dstfp,"%d",a[j]);}fclose(sorcefp);fclose(dstfp);return 0;}
原创粉丝点击