How to make fseek not overwrite what was at the given position when used with fwrite?

I am opening a file with

file = fopen(filename, "r+");

and then use

fseek (file, pos, SEEK_SET);

to go to the position I need, using

fwrite(text, 1, text_size, file);

to write the data.

example:

Say, I want to add '7' at position 3:

abcdef

I want this to be

abc7def
有帮助吗?

解决方案

You can't insert into a file. The only way to accomplish that would be saving the rest of the file somewhere, write your new stuff, then append the rest of the file from where you've saved it. But you have to re-write the whole part of your file after what you want to insert.

其他提示

Fseek won't overwrite anything. You can overwrite what's at the position you seek to if you use fwrite, fputs, fputc or a similar function after the fseek.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top