Ok, I am creating a program that helps a preference file. Now of course, you don’t edit the whole thing, just parts. And not all of these parts are standard “0,1” or “true, false”, some are to the users preference, like the a characters name, which leads me to my problem.
This program is designed to help the user out by displaying specific information from the file, and then being able to edit that specific part. I have heard about the “write ‘something’ to thefile starting at 200”, but since the user can provide information of their own, it will mess up the 200-byte point in the file. So is there any way that I can fix this, like start writing at string “myserver=”?
There’s no way to do it in quite the way you describe, not least of which is that you can’t guarantee that the data you’re writing is exactly the same size (number of characters) as what was previously there, meaning you’ll either have extraneous characters or, even worse, will overwrite following values.
The easier solution would be to read the entire file in as a list (using ‘paragraphs of’…), change the appropriate values, then write all the data back (overwriting the entire file contents).
Alternatively, if all your users will be under Mac OS X, an even better way would be to use the OS-level ‘defaults’ command which writes XML-formatted preferences file, just like you see in your ~/Library/Preferences file. Using this standard format means users are able to edit the file using other XML-compatible utilities, and ‘defaults’ takes care of writing the correct bits of data to the right place in the file.
Note, you should add some error checking to make sure you read in the data correctly, and there’s at least the number of items you expect (you can’t set the fifth item of a 3-item list, for example).
Now this code works exactly as it should, save one thing. It screws up the lines. Real bad. It just smashes everything together and does that till it gets to the line i edited , then It gives 2 lines their own row, and then continues smashing. I don’t know what happened but whatever it was, I really messed it up.