回顾
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fd;
int ws;
int rs;
int fk;
char writebuff[128];
char readbuff[128]={0};
char *str="hwllo wrold";
strcpy(writebuff,str);
fd=fopen("vvvvv","r+");
if(fd==NULL)
{
printf("fopen is error\n");
return -1;
}
printf("fopen is succssed\n");
ws=fwrite(writebuff,4,1,fd);
if(ws<=0)
{
printf("fwrite is error\n");
return -1;
}
printf("fwrite is succssed\n");
fclose(fd);
return 0;
}
因为读写不能同时进行 所以 先把读函数屏蔽了
写函数成功了 再把读函数取消屏蔽。
下面学一个fseek 函数
和之前的lseek 函数有点类似
fseek函数
fseek
long offest:偏移量
int framewhere:起始位置 这样的和lseek函数的 形参 int whence
意思是一样的
不理解回去看看。
直接写代码
先用 SEEK_SET :一开头为起始位置
配合读写看效果
重新编译下载写函数 (注意很重要 )
把这步记为第一步
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fd;
int ws;
int rs;
int fk;
char writebuff[128];
char readbuff[128]={0};
char *str="hwllowrold";
strcpy(writebuff,str);
fd=fopen("vvvvv","w+");
if(fd==NULL)
{
printf("fopen is error\n");
return -1;
}
printf("fopen is succssed\n");
ws=fwrite(writebuff,4,2,fd);
if(ws<=0)
{
printf("fwrite is error\n");
return -1;
}
printf("fwrite is succssed\n");
fclose(fd);
return 0;
}
因为 fseek 配合 fread fwrite 使用 效果会明显
在 fwrite 基础 上 加入fread 并且把 w+ 改为 r+
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fd;
int ws;
int rs;
int fk;
char writebuff[128];
char readbuff[128]={0};
char *str="hwllowrold";
strcpy(writebuff,str);
fd=fopen("vvvvv","r+");
if(fd==NULL)
{
printf("fopen is error\n");
return -1;
}
printf("fopen is succssed\n");
rs=fread(readbuff,4,2,fd);
if(ws<=0)
{
printf("fread is error\n");
return -4;
}
printf("fread is %s\n",readbuff);
ws=fwrite(writebuff,4,2,fd);
if(ws<=0)
{
printf("fwrite is error\n");
return -1;
}
printf("fwrite is succssed\n");
fclose(fd);
return 0;
}
fseek 函数 的 SEEK_SET 的用处
要点 2
为什么出现查看 写的内容多了一倍呢 因为在读的时候,读到后面 又是写的开始
因为没有光标把它移动到第一个位置 重新写入
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fd;
int ws;
int rs;
int fk;
char writebuff[128];
char readbuff[128]={0};
char *str="hwllowrold";
strcpy(writebuff,str);
fd=fopen("vvvvv","r+");
if(fd==NULL)
{
printf("fopen is error\n");
return -1;
}
printf("fopen is succssed\n");
rs=fread(readbuff,4,2,fd);
if(ws<=0)
{
printf("fread is error\n");
return -4;
}
printf("fread is %s\n",readbuff);
fk=fseek(fd,0,SEEK_SET);
if(fk)
{
printf("fseek is error\n");
return -55;
}
ws=fwrite(writebuff,4,2,fd);
if(ws<=0)
{
printf("fwrite is error\n");
return -1;
}
printf("fwrite is succssed\n");
fclose(fd);
return 0;
}
SEEK_SET :以开始为起始地址 即第0个位置
fseek 函数 的 SEEK_END 的用处
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fd;
int ws;
int rs;
int fk;
char writebuff[128];
char readbuff[128]={0};
char *str="hwllowrold";
strcpy(writebuff,str);
fd=fopen("vvvvv","r+");
if(fd==NULL)
{
printf("fopen is error\n");
return -1;
}
printf("fopen is succssed\n");
rs=fread(readbuff,4,2,fd);
if(ws<=0)
{
printf("fread is error\n");
return -4;
}
printf("fread is %s\n",readbuff);
fk=fseek(fd,8,SEEK_END);
if(fk)
{
printf("fseek is error\n");
return -55;
}
ws=fwrite(writebuff,4,2,fd);以读的结尾开始写入 4*2 长度
if(ws<=0)
{
printf("fwrite is error\n");
return -1;
}
printf("fwrite is succssed\n");
fclose(fd);
return 0;
}
SEEK_END:读的尾部 (写结果多了一倍)
fseek 函数 的 SEEK_CUR 的用处
因为我连用了 SEEK_END SEEK_SET 导致现在的读的尾部最后到了15
那我应该 SEEK_CUR
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fd;
int ws;
int rs;
int fk;
char writebuff[128];
char readbuff[128]={0};
char *str="hwllowrold";
strcpy(writebuff,str);
fd=fopen("vvvvv","r+");
if(fd==NULL)
{
printf("fopen is error\n");
return -1;
}
printf("fopen is succssed\n");
rs=fread(readbuff,4,2,fd);
if(ws<=0)
{
printf("fread is error\n");
return -4;
}
printf("fread is %s\n",readbuff);
fk=fseek(fd,16,SEEK_CUR);
if(fk)
{
printf("fseek is error\n");
return -55;
}
ws=fwrite(writebuff,4,2,fd);
if(ws<=0)
{
printf("fwrite is error\n");
return -1;
}
printf("fwrite is succssed\n");
fclose(fd);
return 0;
}
SEEK_CUR :以当前 xxx 为起点 fwrite (4*2) 往后写入 8个长度
连续使用 注意偏移问题。
注意读出到了哪个位置
其实参数写对了 一直就是被假象给骗了 因为读的位置一直在最后
而你的偏移却没有写对 导致一直覆盖 其实想法是对了
读完之后一定在偏移到字符最后
如果不用lseek fseek
重新把位置偏移到某个位置
写也会在读的结尾继续输入
|