开发者

Inserting data at a particular position in file using fseek() in C

Basically I want to write data to a file at a particular position and don't wa开发者_Go百科nt to load data in to the memory for sorting it. For example if I have in a file:

FILE.txt

Andy dsoza
Arpit Raj
Karishma Shah
Pratik Mehta
Zppy andre

And i want to insert a contact Barbie patel then i will read the first letter in the file after every line , so Barbie should be inserted after Arpit and before Karishma so the file after editing should be:

FILE.txt

Andy dsoza
Arpit Raj
Barbie Patel
Karishma Shah
Pratik Mehta
Zppy andre

But fseek drives me to that postion but dosen't help me insert when i use fprintf/fwrite/putc. It replaces the byte but does not insert before that particular byte.

Loading all the data in to the memmory and sorting it out would not be good if i wold have lot of contacts in future.


You won't be able to directly insert into a file without loading it into memory. How you are to manage longer file depends on efficient design approach.

One approach would be to use different files.


You cannot insert data in the middle of a file. You have to first read everything that's in the file from that point to the end, overwrite and then append what you read.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜