关于对文件的读写

molder 2003-10-09 10:26:03
请教用C的文件读写函数在对文件进行读写时,是不是也可以指定文件的打开模式:只读、共享、独占?因为当我在对一个文件进行写操作的时候,此时读该文件的结果为0,请帮我解决这个问题:当对一个文件进行写操作的时候,如何能读出该文件的内容。谢谢!!
...全文
28 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jxdsp 2003-10-10
  • 打赏
  • 举报
回复
可以先“a+”读内容保存
再“w+”写呀
huigll 2003-10-09
  • 打赏
  • 举报
回复
如果用"w" "w+"打开文件时破坏了文件的内容,所以用这两种方式打开文件再用另一个文件指针读文件时文件是空的。
"r"

Opens for reading. If the file does not exist or cannot be found, the fopen call fails.

"w"

Opens an empty file for writing. If the given file exists,
its contents are destroyed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~
"a"

Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn’t exist.

"r+"

Opens for both reading and writing. (The file must exist.)

"w+"

Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.
~~~~~~~~~~~~~~~~~~~~~~~
"a+"

Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn’t exist.

所以只读模式就是"r"

共享模式可以认为是"r+" "a+"
可以在写文件时读文件原来的内容。改变后的内容要等关闭后才可以读出来(用另一个文件指针)。
hamsky 2003-10-09
  • 打赏
  • 举报
回复
纯C是运行在DOS下的,单任务的,应该是独占,要想有写有读的只能自己调整文件文件指针的位置啦

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧