C语⾔⽂件读写Demo CIODemo.c
#include <stdio.h>
#include <time.h>
#define INPUT_BUFFER_SIZE 100 * 1024
int main()
{
printf("Enter an input file name: ");
char filename[1000];
gets(filename);
FILE *inputFile = fopen(filename, "rb");
if (inputFile)
{
printf("Enter an output file name: ");
gets(filename);
FILE *outputFile = fopen(filename, "wb");
if (outputFile)
{
printf("\n");
time_t startTime = time(NULL);
char buffer[INPUT_BUFFER_SIZE];
while (!feof(inputFile))
{
size_t numberOfObjects = fread(buffer, sizeof(char), INPUT_BUFFER_SIZE, inputFile);
fwrite(buffer, sizeof(char), numberOfObjects, outputFile);
}
time_t endTime = time(NULL);
printf("File copied, elapsed time: %u seconds\n", endTime - startTime);
fclose(inputFile);
fclose(outputFile);
}
else
printf("Cannot open file: %s", filename);
}
else
printf("Cannot open file: %s", filename);
return0;
inputtypefile样式}
以上为读写⼆进制⽂件的⽰例,若要读写⽂本⽂件,只需将⽂件访问模式更改为r(读)和w(写)即可

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。