开发者

When opening a file in append mode, how can I reposition the file pointer?

开发者 https://www.devze.com 2023-01-22 02:26 出处:网络
I am trying to insert some data into the middle of a file. I have opened the file in append mode as: file = fopen(msg->header.filename, \"ab\");

I am trying to insert some data into the middle of a file. I have opened the file in append mode as:

file = fopen(msg->header.filename, "ab");

I then tried a seek to the desired offset in the file as so:

fseek(file, msg->header.offset, SEEK_SET);

However, when I then try an fwrite as so:

int bytesWritten = fwrite(msg->message, 1, msg->header.length, file);

All the data is written to the end of the file instead of in the middle of the file.

Is this because I am using append mode? I would open in write mode, but I need to keep the 开发者_运维知识库existing contents in the file.


Look at the specification of ANSI C function fopen for "a" (APPEND) mode: All write operations take place at the end of the file. Your fseek will ignored.

0

精彩评论

暂无评论...
验证码 换一张
取 消