#include <stdio.h>
//txt:abcdef
int main ()
{
FILE *fp = fopen("txt", "r");
if(fp == NULL)
{
perror("open error\n");
return -1;
}
char p[10] = {0};
fread(p, 5, 2, fp); //每次读5个,读2遍, 在fp里面读,传给p
printf("%s\n", p); //abcdef
fclose(fp);
return 0;
}
#include <stdio.h>
//txt:abcdef
int main ()
{
FILE *fp = fopen("txt", "r+");
if(fp == NULL)
{
perror("open error\n");
return -1;
}
fwrite("mm", 2, 1, fp); //会偏移2个
fseek(fp, 0, SEEK_SET);
//SEEK_SET在开头的第几个开始输出
//SEEK_CUR在当前的位置的第几个开始输出
//SEEK_END在末尾的第几个开始输出
char p[10] = {0};
fread(p, 5, 2, fp); //每次读5个,读2遍, 在fp里面读,传给p
printf("%s\n", p); //mmcdef
fclose(fp);
return 0;
}
#include <stdio.h>
//txt:abcdef
int main ()
{
//FILE *fp = fopen("txt", "w");
//可以写入不能输出。
//如果没有txt文件,会自动创建再写入。
//如果有txt文件,会自动把txt文件内容清空
FILE *fp = fopen("txt", "w+"); //可以写入和输出,其他同上。
if(fp == NULL)
{
perror("open error\n");
return -1;
}
fwrite("mm", 2, 1, fp); //会偏移2个
fseek(fp, 0, SEEK_SET);
//SEEK_SET在开头的第几个开始输出
//SEEK_CUR在当前的位置的第几个开始输出
//SEEK_END在末尾的第几个开始输出
char p[10] = {0};
fread(p, 5, 2, fp); //每次读5个,读2遍, 在fp里面读,传给p
printf("%s\n", p); //mm
fclose(fp);
return 0;
}
#include <stdio.h>
//txt:abcdef
int main ()
{
//FILE *fp = fopen("txt", "a");
//可以追加内容不能读。
//如果没有txt文件,会自动创建再写入。
//如果有txt文件,会自动再尾端追加。
FILE *fp = fopen("txt", "a+"); //可以追加内容和读,其他同上。
if(fp == NULL)
{
perror("open error\n");
return -1;
}
fwrite("mm", 2, 1, fp); //会偏移2个
fseek(fp, 0, SEEK_SET);
//SEEK_SET在开头的第几个开始输出
//SEEK_CUR在当前的位置的第几个开始输出
//SEEK_END在末尾的第几个开始输出
char p[10] = {0};
fread(p, 5, 2, fp); //每次读5个,读2遍, 在fp里面读,传给p
printf("%s\n", p); //abcdefmm
fclose(fp);
return 0;
}