打开“a”中的文件;模式

发布于 2024-09-18 03:47:10 字数 338 浏览 4 评论 0原文

如果使用以下命令打开文件:

FILE *f1=fopen("test.dat","a+");

手册页显示:

a+

打开以进行读取和追加(写入文件末尾)。这 如果文件不存在则创建该文件。初始文件位置 读取位于文件的开头,但输出是 始终附加到文件末尾。

那么 f1 是否有 2 个独立的偏移指针,一个用于读取和一个偏移指针。另一个用于写?

If a file is opened using the following command:

FILE *f1=fopen("test.dat","a+");

The man page reads:

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.

So does f1 have 2 separate offset pointers, one for read & another for write?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云之铃。 2024-09-25 03:47:10

不会。

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

No.

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.

甜警司 2024-09-25 03:47:10

不,它只有一个指针。

No it has only one pointer.

孤独患者 2024-09-25 03:47:10

永远不能在FILE上混合读取和写入操作,而无需在其间调用fseek。它可能在某些实现上按您希望的方式工作,但是依赖于此的程序具有未定义的行为。因此,拥有2个位置的问题是没有意义的。

You can never mix reading and writing operations on a FILE without calling fseek in between. It may work as you wish on some implementations, but a program that depends on this has undefined behavior. Thus the questions of having 2 positions is meaningless.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文