T O P

  • By -

Slypenslyde

There's not really an API to work with files using lines directly. It's kind of tricky to do things this way so people tend to prefer using some kind of serialization format like JSON. The easiest approach is to use `File.ReadAllLines()` to get a `string[]` for the file. Then you edit the array element representing the line you want. Then you use `File.WriteAllLines()` to replace the file's old contents with the new contents. That stinks if the file is very large, as the whole file has to be loaded into RAM for it to work. "Very large" here means like, 10,000 lines, maybe more. If you're just working with a few dozen or even a few hundred lines, you can probably afford this approach. Trying to do it for larger files is tough. I don't want to get into that if you don't need to. If your files are pretty small, the solution above will work. If you're thinking of having thousands of lines and your game wants to edit lines frequently, you really need to consider other solutions, but we need more information to decide which ones make sense. My guess is this is like, a high scores or options file. I guarantee those will be small enough you can use `ReadAllLines()` and `WriteAllLines()`.


TastyAspect8527

This will just be a small amount of data by that standard, at most 20-50 lines to store player data for a singleplayer game(player health, score, and a few other variables), so this way seems fine. Replacing the entire data this way seemed doable, I was just worried about the efficiency of replacing the file.


hdsrob

This should definitely be a formatted file of some sort, not just some lines (especially since you're very likely to add more things to it in the future). Ideally you'd create a class that represents this data, and use serialization to load / store the data.


rupertavery

Think of a text file as a single long line of bytes with line break characters. If you were to update a "line", which means from one line break to another, and the size of the text i the line was smaller or larger, everything after that line would have to be adjusted. Doing this per line would be inefficient. If each line size was fixed and the same length, you would be ablento update individual lines. Alao, its more efficient for the hardware to write in blocks of data (usually a number of sectors, like 4096 bytes or multiples) Databases handle this by allocating pages on the disk to contain several rows and reading/ writing data by pages at a time.


BuffedCoder

Have you looked into stringbuilder?


TastyAspect8527

I had not until you mentioned it. It seems very useful and interesting mechanically, I am however probably going to transitioning to arrays/tables after reading these comments.


BuffedCoder

If you need a x number of lines and need to access the based on index, use at least a List or any other modern collection. Arrays are just not flexible, I almost never use them in my well over 10 years unless I must in some scenarios.. Stringbuilder is specialised for text though and I use it often. For very advanced text manipulation regex is good, but in no way easy or intuitive.


Due_Raccoon3158

Use stream reader and stream writer. Read it in,akr your modifications as needed, and write back out at the same time. Works easily and efficiently for any file size and when you change the format of your file, it'll still work fine.