Pergunta

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
Foi útil?

Solução

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.

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top