免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1417 | 回复: 6
打印 上一主题 下一主题

文件寫入問題 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-11-25 19:06 |只看该作者 |倒序浏览
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int main (int argc, char *argv[])
  4. {
  5.     int i;
  6.     char s[100];
  7.     FILE *db;
  8.    
  9.     db = fopen ("./db.txt", "r+b");
  10.    
  11.     fgets (s, 100, db);
  12.     fseek (db, 0, SEEK_CUR);
  13.     fputs (s, db);
  14.    
  15.     return (0);
  16. }
复制代码


上面的代碼中, 為什麼刪去了 fseek (db, 0, SEEK_CUR);
就不能寫入文件???

论坛徽章:
0
2 [报告]
发表于 2005-11-25 19:11 |只看该作者
原帖由 暴BB 于 2005-11-25 19:06 发表
[code]#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
    int i;
    char s[100];
    FILE *db;
   
    db = fopen ("./db.txt", "r+b ...



db = fopen ("./db.txt", "r+b ");
这可以写入嘛?
呵呵

论坛徽章:
0
3 [报告]
发表于 2005-11-25 19:57 |只看该作者
樓上的找本書看看再回答.....=.=

等待高手回答中...

论坛徽章:
0
4 [报告]
发表于 2005-11-25 19:59 |只看该作者
原帖由 暴BB 于 2005-11-25 19:57 发表
樓上的找本書看看再回答.....=.=

等待高手回答中...


haha, 好。
你个人真有意思。

论坛徽章:
0
5 [报告]
发表于 2005-11-25 20:17 |只看该作者
man fopen

Reads and writes may be intermixed on read/write streams in any order.  Note that ANSI C requires that
       a file positioning function intervene between output and input, unless an input  operation  encounters
       end-of-file.   (If  this  condition  is not met, then a read is allowed to return the result of writes
       other than the most recent.)  Therefore it is good practice  (and  indeed  sometimes  necessary  under
       Linux)  to put an fseek or fgetpos operation between write and read operations on such a stream.  This
       operation may be an apparent no-op (as in fseek(..., 0L, SEEK_CUR) called for its  synchronizing  side
       effect.

论坛徽章:
0
6 [报告]
发表于 2005-11-25 21:31 |只看该作者
传说中的高手终于出现了

------------------------

论坛徽章:
0
7 [报告]
发表于 2005-11-26 09:56 |只看该作者
fseek的作用是清楚FILE中的某些标志,而且在读写之间刷新缓冲,不然的话会出现错误.
更多的细节看fseek的的代码比较好. 不然说了也没有用
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP