【问题标题】:does append "a+" automatically put cursor to the end是否附加“a+”会自动将光标放在末尾
【发布时间】:2014-04-15 15:03:23
【问题描述】:

我想用 c 打开一个文件,然后添加一些内容并关闭它。我只想知道 fopen 中的 a+ 会自动导航到文件的最后一个字符。

【问题讨论】:

  • 只需阅读“fopen”的文档,例如这里:cplusplus.com/reference/cstdio/fopen
  • 如果你不喜欢这本手册,你可以这样做ftell(f,0,SEEK_CUR);
  • 这个问题可以通过编写两个程序来改进,一个使用“a+”,一个使用“a”,并比较运行每个程序的结果。然后,如果您想作为“回答您自己的问题”的问题,您可以将其提交给 SO

标签: c file-io


【解决方案1】:

是的。

您为什么不尝试一下,或者阅读一下手册?

这里是:

   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 beginning 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.

【讨论】:

  • 回答这个问题对用户没有好处。他应该先学会阅读文档,或者在他在这里提问之前做一个简单的网络搜索。
  • 这是真的,但是,嘿,这就是堆栈溢出的意义所在……至少人们在网络搜索中会发现这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-20
  • 1970-01-01
  • 2011-12-17
  • 2015-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多