fseek逆序复制文件的c代码
时间: 2023-11-30 18:40:08 浏览: 27
```c
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp1, *fp2;
long int size;
char *buffer;
fp1 = fopen("input.txt", "rb");
if (fp1 == NULL)
{
printf("Error opening file\n");
exit(1);
}
fseek(fp1, 0, SEEK_END);
size = ftell(fp1);
fseek(fp1, 0, SEEK_SET);
buffer = (char*)malloc(size * sizeof(char));
if (buffer == NULL)
{
printf("Memory allocation error\n");
exit(2);
}
fread(buffer, sizeof(char), size, fp1);
fclose(fp1);
fp2 = fopen("output.txt", "wb");
if (fp2 == NULL)
{
printf("Error opening file\n");
exit(1);
}
for (int i = size - 1; i >= 0; i--)
{
fwrite(&buffer[i], sizeof(char), 1, fp2);
}
fclose(fp2);
free(buffer);
return 0;
}
```
--相关问题--:
1. fseek函数的作用是什么?
2. 如何在C语言中