操作文件夹
#include<stdio.h>
int main(void)
{
char filename[100];
static int num=1;
snprintf(filename,100,"day_%d",num);
FILE* fp;
fp = fopen(filename,"rb");
if(!fp){
printf("error open fp\n");
}
fseek(fp,20,SEEK_SET);
//fseek(fp,0,SEEK_END);
int len = ftell(fp);
char buffer[100];
fseek(fp,0,SEEK_SET);
fread(buffer,len,1,fp);
fclose(fp);
FILE* fp_r;
fp_r = fopen("day","w");
if(!fp_r){
printf("error open fp_r\n");
}
fwrite(buffer,len,1,fp_r);
fclose(fp_r);
return 0;
}
2)
int main(int argc, char* argv[])
{
char *read = (char *)malloc(100);
FILE *fp;
fp = fopen("a.txt","a+");//contain "Julie"
if(fp == NULL)
printf("fopen error\n");
//fread related with the fp location
fseek(fp,0,SEEK_SET);
if(fread(read,5,1,fp)!=1)
printf("fread error\n");
sprintf(read+5,"%.14s","love me tender");
int len = ftell(fp);
if(len){
printf("now the length is %d\n",len);
fseek(fp,0,SEEK_END);
}
//fwrite not related with fp location
if(fwrite(read,strlen(read),1,fp)!=1)
printf("fwrite failed\n");
printf("%s",read);
printf("\n");
fclose(fp);
return 0;
}
本文介绍了使用C语言进行文件读写的两个实例。第一个实例展示了如何打开多个文件、读取内容并将其写入另一个文件中;第二个实例则演示了如何在文件末尾追加内容,并在指定位置修改文件内容。
1825

被折叠的 条评论
为什么被折叠?



