免费注册 查看新帖 |

Chinaunix

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

请教:fopen的文件使用方式"a"的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-10-13 17:18 |只看该作者 |倒序浏览
有个数据文件powermeter,假设原来为10KByte。
我要从第100Byte到第199Byte改写信息,还要在文件开始处的第2000Byte开始追加100Byte的信息。

我之前的做法是:
fp=fopen(filename,"a");
fseek(fp,0,SEEK_SET);//我以为此时光标位置是文件开头,结果发现光标位置是文件的末尾,也就是说“a”只能以追加方式写,只能定位到原文件末尾,而不能是原文件中间的某个位置


请问:我现在需要改写原文件,又要追加信息,该怎么办?

先谢谢啦!

论坛徽章:
84
每日论坛发贴之星
日期:2015-12-29 06:20:00每日论坛发贴之星
日期:2016-01-16 06:20:00每周论坛发贴之星
日期:2016-01-17 22:22:00程序设计版块每日发帖之星
日期:2016-01-20 06:20:00每日论坛发贴之星
日期:2016-01-20 06:20:00程序设计版块每日发帖之星
日期:2016-01-21 06:20:00每日论坛发贴之星
日期:2016-01-21 06:20:00程序设计版块每日发帖之星
日期:2016-01-23 06:20:00程序设计版块每日发帖之星
日期:2016-01-31 06:20:00数据库技术版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-14 06:20:00
2 [报告]
发表于 2009-10-13 22:24 |只看该作者

回复 #1 bobzone 的帖子

fopen("filename", "r+");

man fopen

论坛徽章:
0
3 [报告]
发表于 2009-10-13 23:36 |只看该作者
用rw打开,然后用seek定位就行了

论坛徽章:
84
每日论坛发贴之星
日期:2015-12-29 06:20:00每日论坛发贴之星
日期:2016-01-16 06:20:00每周论坛发贴之星
日期:2016-01-17 22:22:00程序设计版块每日发帖之星
日期:2016-01-20 06:20:00每日论坛发贴之星
日期:2016-01-20 06:20:00程序设计版块每日发帖之星
日期:2016-01-21 06:20:00每日论坛发贴之星
日期:2016-01-21 06:20:00程序设计版块每日发帖之星
日期:2016-01-23 06:20:00程序设计版块每日发帖之星
日期:2016-01-31 06:20:00数据库技术版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-14 06:20:00
4 [报告]
发表于 2009-10-14 12:26 |只看该作者
原帖由 emmoblin 于 2009-10-13 23:36 发表
用rw打开,然后用seek定位就行了


提醒:fopen 没有 rw ,用rw和r一样;读写是r+

DESCRIPTION
       The fopen() function opens the file whose name is the string pointed to by path and associates a stream with it.
       The argument mode points to a string beginning with one of the following sequences (Additional characters may follow these sequences.):
       r      Open text file for reading.  The stream is positioned at the beginning of the file.
       r+     Open for reading and writing.  The stream is positioned at the beginning of the file.
       w      Truncate file to zero length or create text file for writing.  The stream is positioned at the beginning of the file.
       w+     Open for reading and writing.  The file is created if it does not exist, otherwise it is truncated.  The stream is positioned at the begin-
              ning of the file.
       a      Open for appending (writing at end of file).  The file is created if it does not exist.  The stream is positioned at the end of the file.
       a+     Open for reading and appending (writing at end of file).  The file is created if it does not exist.  The initial file position for  reading
              is at the beginning of the file, but output is always appended to the end of the file.

论坛徽章:
0
5 [报告]
发表于 2009-10-14 17:53 |只看该作者
rw记得可以用

论坛徽章:
84
每日论坛发贴之星
日期:2015-12-29 06:20:00每日论坛发贴之星
日期:2016-01-16 06:20:00每周论坛发贴之星
日期:2016-01-17 22:22:00程序设计版块每日发帖之星
日期:2016-01-20 06:20:00每日论坛发贴之星
日期:2016-01-20 06:20:00程序设计版块每日发帖之星
日期:2016-01-21 06:20:00每日论坛发贴之星
日期:2016-01-21 06:20:00程序设计版块每日发帖之星
日期:2016-01-23 06:20:00程序设计版块每日发帖之星
日期:2016-01-31 06:20:00数据库技术版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-14 06:20:00
6 [报告]
发表于 2009-10-14 21:30 |只看该作者
原帖由 @sky 于 2009-10-14 17:53 发表
rw记得可以用


记错了吧,至少标准C里没有,,

论坛徽章:
0
7 [报告]
发表于 2009-10-26 19:39 |只看该作者

回复 #2 yjh777 的帖子

谢谢大家。新手就是新手,还没有养成man的习惯。
吸取教训。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP