如果文件是在“a+b”中打开的,fseek() 是否将文件指针移动到文件的开头?模式?
我希望使用“a+b”模式打开一个文件,即如果它不存在,则会自动创建,但如果存在,我不想覆盖它。我希望能够读取和写入该文件。
该文件是二进制的,我想在其中保存特定struct
的记录。所以我想对我想要的记录执行fseek()
,然后使用fwrite()
保存记录。
代码如下所示(MyRecord
是 struct
的 typedef
,而 FILENAME
是 #定义
到文件名):
int saveRecord(MyRecord *pRecord, int pos)
{
FILE* file = fopen(FILENAME, "a+b");
if (file == NULL)
{
printf("Unable to open file %s\n", FILENAME);
return 0;
}
fseek(file, pos * sizeof(MyRecord), SEEK_SET);
fwrite(pRecord, sizeof(MyRecord), 1, file);
fclose(file);
return 1;
}
但是,即使我将 pos
设置为 0,此代码也只是将记录附加到文件末尾。为什么不是 fseek()
和 SEEK_SET
在附加模式下工作?
我知道我可以简单地用“r+b”打开它,如果失败,用“wb”打开它,但我想知道为什么这不起作用以及为什么 fseek()
与 SEEK_SET
将文件指针保留在末尾。任何对记录此行为的地方的引用表示赞赏(因为我找不到任何内容,或者我使用了错误的关键字)。
I wish to open a file using the "a+b" mode, i.e. if it does not exist it is created automatically, but if it does I don't want to overwrite it. I want to be able to read and write to the file.
The file is binary, and I want to save records of a specific struct
in it. So I want to do fseek()
to the record I want and then save the record using fwrite()
.
The code looks as follows (MyRecord
is a typedef
to a struct
, while FILENAME
is a #define
to the file's name):
int saveRecord(MyRecord *pRecord, int pos)
{
FILE* file = fopen(FILENAME, "a+b");
if (file == NULL)
{
printf("Unable to open file %s\n", FILENAME);
return 0;
}
fseek(file, pos * sizeof(MyRecord), SEEK_SET);
fwrite(pRecord, sizeof(MyRecord), 1, file);
fclose(file);
return 1;
}
However this code just appends the record to the end of the file, even if I set pos
to 0. Why isn't fseek()
with SEEK_SET
working in append mode?
I know I can simply open it with "r+b" and if it fails open it with "wb", but I want to know why this doesn't work and why fseek()
with SEEK_SET
is leaving the file pointer at the end. Any references to places where this behaviour is documented appreciated (because I couldn't find any, or I am using the wrong keywords).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为在
a
模式下,写入FILE*
总是附加到末尾。fseek
在此模式下仅设置读指针。这在 C 标准 7.19.5.3 fopen 中有记录:That's because in
a
mode, writing to theFILE*
always appends to the end.fseek
only sets the read pointer in this mode. This is documented in the C standard, 7.19.5.3 fopen:普通 C 没有任何明智的方法来实现你想要的。如果您使用的是 POSIX 系统或任何远程关闭的系统,则可以使用
fd=open(FILENAME, O_CREAT|O_RDRW, 0666)
,然后使用fdopen(fd, "rb+")
代码>.编辑:您可以尝试使用普通 C 的另一件事:
Plain C does not have any sane way to achieve what you want. If you're on a POSIX system or anything remotely close, you can use
fd=open(FILENAME, O_CREAT|O_RDRW, 0666)
and thenfdopen(fd, "rb+")
.Edit: Another thing you could try, with plain C:
使用“r+b”模式,如果失败则回退到“w+b”。
“a+b”模式,允许您读取和追加; “r+b”允许随机读写。
fopen
的文档描述了该文件如何具有不同模式的行为。Use "r+b" mode and fallback to "w+b" if it fails.
The "a+b" mode, allows you to read and append; the "r+b" allows random read and write.
The documentation for
fopen
describes how the file behaves with the different modes.