示例
图片附件: 游客没有浏览图片的权限,请
登录 或
注册
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
FILE *fp;
int fsize;
char *buf;
if ((fp=fopen ("test.txt","wt")) == NULL)
exit(0);
fputs("1234567890\n", fp);
fputs("ABCDEFGHIJ\n", fp);
fclose(fp);
if ((fp=fopen ("test.txt","rb+")) == NULL)
exit(0);
fseek(fp, 0, SEEK_END);
fsize = ftell(fp);
buf = (char *)malloc(fsize+1);
if (buf == NULL)
{
fclose(fp);
exit(0);
}
fseek(fp, 0, SEEK_SET);
fread(buf, fsize, 1, fp);
buf[fsize] = '\0';
printf("%s\n", buf);
char *p;
if ((p=strstr(buf,"DEFG")) != NULL)
{
memcpy(p, "defg", 4);
}
fseek(fp, 0, SEEK_SET);
fwrite(buf, fsize, 1, fp);
fseek(fp, 0, SEEK_SET);
fread(buf, fsize, 1, fp);
buf[fsize] = '\0';
printf("%s\n", buf);
fclose(fp);
free(buf);
return 0;
}