菜鸟笔记
提升您的技术认知

linux的fseek函数-ag真人游戏

回顾

 #include
#include
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");  
   /*
   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,1,fd);//写函数 
   if(ws<=0)//写错误
   {
  
      printf("fwrite is error\n");
      return -1;
   }
    printf("fwrite is succssed\n");
  fclose(fd);
  return 0;
}
因为读写不能同时进行 所以 先把读函数屏蔽了 
写函数成功了 再把读函数取消屏蔽。
下面学一个fseek 函数 
和之前的lseek 函数有点类似 
fseek 
long offest:偏移量
int framewhere:起始位置 这样的和lseek函数的 形参 int whence
意思是一样的 

不理解回去看看。

直接写代码  
先用 seek_set :一开头为起始位置
配合读写看效果 

重新编译下载写函数 (注意很重要 )

把这步记为第一步 
#include
#include
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 ");//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
#include
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 ");//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;
}

要点 2

为什么出现查看 写的内容多了一倍呢  因为在读的时候,读到后面 又是写的开始 
因为没有光标把它移动到第一个位置 重新写入 
#include
#include
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);//读完之后 把地址偏移到 开始 第0 的位置 
    
    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_set :以开始为起始地址 即第0个位置 
#include
#include
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);//以读的结尾 增加8个偏移
    
    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:读的尾部 (写结果多了一倍)

因为我连用了 seek_end seek_set 导致现在的读的尾部最后到了15
那我应该 seek_cur 
 #include
#include
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); //16位当前的新地址
    
    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_cur :以当前 xxx  为起点  fwrite (4*2) 往后写入 8个长度 
连续使用 注意偏移问题。
注意读出到了哪个位置 
其实参数写对了 一直就是被假象给骗了 因为读的位置一直在最后
 而你的偏移却没有写对 导致一直覆盖 其实想法是对了 
读完之后一定在偏移到字符最后 
如果不用lseek fseek 
重新把位置偏移到某个位置 
写也会在读的结尾继续输入
网站地图