1、读文件file1.txt的内容(例如):
12
34
56
输出到file2.txt:
56
34
12
34
56
输出到file2.txt:
56
34
12
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;
int MAX = 10;
int main()
{
int *a=(int *)malloc(MAX*sizeof(int));
int *b=NULL; //作为扩增临时替代空间
FILE *fp1 ;
FILE *fp2 ;
fopen_s(&fp1,"f1.txt", "r");
fopen_s(&fp2 ,"f2.txt", "w");
if (fp1 == NULL | fp2 == NULL) return -1;
int i = 0;
int j = 0;
while (fscanf_s(fp1, "%d", &a[i], 100) != EOF) //100这里需要更改
{
i++;
j++;
if (i >= MAX)
{
MAX = 2 * MAX;
b = (int*)realloc(a, MAX * sizeof(int));
if (b == NULL) return -1;
a = b;
}
}
for (; --j >= 0;)
{
fprintf(fp2, "%d\n", a[j]);
}
fclose(fp1);
fclose(fp2);
fp1 = NULL; //需要指向空,否则会指向原打开文件地址
fp2 = NULL;
return 0;
}