Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
686 views
in Technique[技术] by (71.8m points)

cocoa - 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 seekToFileOffsetand update the file. please help me out.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...