// file.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int size;
/*读文件file1.txt的内容(例如)
12
34
56
输出到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;
}