从使用追加模式打开的文件中读取

编程入门 行业动态 更新时间:2024-10-26 20:27:49
本文介绍了从使用追加模式打开的文件中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这可能是一个非常愚蠢的问题,但是我正在修改其他人的代码,看来我需要从以追加模式打开的文件中读取内容.我试图

It might be a very dumb question but I am modifying someone else's code and it seems I need to read from a file that was opened in append mode. I tried to fseek to the beginning of the file but nothing is being read. I know I can change the mode to rw but I wanted to know why fseek is not working. In the man page it does say write ignores fseek but nothing about read though.

推荐答案

只有一个指针,该指针最初位于文件的开头,但是当尝试执行写操作时,它将移动到文件的末尾.您可以使用fseek对其进行重新定位,也可以在文件中的任何位置后退以进行读取,但是写入操作会将其移回到文件末尾.

There is just one pointer which initially is at the start of the file but when a write operation is attempted it is moved to the end of the file. You can reposition it using fseek or rewind anywhere in the file for reading, but writing operations will move it back to the end of file.

在追加模式下打开时,文件指针将在每次写入之前返回到文件末尾.您可以使用fseek重新定位指针以进行读取,但是一旦调用写入文件的函数,指针就会回到文件末尾.

When you open in append mode, the file pointer is returned to the end of file before every write. You can reposition the pointer with fseek for reading, but as soon as you call a function that writes to the file, the pointer goes back to the end of file.

在如果文件指针是在"a + b"目录中打开的,fseek()是否将文件指针移动到文件的开头?模式?引用了C标准的相应部分.

The answer at Does fseek() move the file pointer to the beginning of the file if it was opened in "a+b" mode? references the appropriate section of the C standard.

如果要写入文件中的任意位置,请使用"w +"模式.现有文件将被覆盖.

Use the "w+" mode if you would like to write to arbitrary places in file. An existing file will be overwritten.

如果您想先附加到现有文件,然后再查找到任意位置,请使用"r +"后跟

If you would like to append to an existing file initially, but then fseek to arbitrary place, use "r+" followed by

fseek(f, 0, SEEK_END)

希望有帮助.

更多推荐

从使用追加模式打开的文件中读取

本文发布于:2023-10-08 13:58:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472834.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!