简体   繁体   中英

Inserting a string at a specified line in text file using objective c

Lets say I have a text file with the following data

Username

User ID

Details

suppose, I want to insert a new line "User screenname" inbetween "User ID" and "Details". How should I do this ? Is it possible to insert string at a specific line ? since these have a variable string length for different users, i think its not possible to use seekToFileOffset and update the file. please help me out.

Thanks

The only way to do this is to read the file into memory, modify the internal representation of the data, and write it back out.

There are some variations on this process, but in short; files are not like real paper and ink documents: You can't insert content into the middle of a file, only replace what is already there.

Edit: To clarify, you can only grow a file at the end; not in the middle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM