I am working on my uni project on C#. It requires me to create a student management system in C# console. I have to use text file for saving data. I can add data and retrieve data in text file but unable to update any student record. My question is how I can update specific student record in text file? For example my program will ask user to input student that he wants to update than program should fetch the complete the record of that student. The program again ask user to select the field or fields that he wants to update. After updating the field the record should be updated.
I can suggest some simple/obvious and not the most efficient way.
For the homework, it doesn't have any performance problem that prevents you from loading all students data to a big array and then updating the array. When everything is done, and user choose to quit, you can save all the data.
The harder way is to load the student up and update the student object then writing it back to the end of the file. Don't forget to delete the old row as well. You can store each student in a row
Another harder way is to random access the file, requiring a strict text data format
If you are not required to do text output but use a file that is hand editable, you should heavily consider creating an Xml Serializable class say Data that implements a List where Student is serializable. Then you just have to the Xml Serialize/Deserialize methods to read and write your data. Once read in you can implement a find algorithm or use Linq on the List. Of course @vodkhang mentions a valid point about file storage regarding performance and random access vs loading all into memory.
You could also use the new MemoryMappedFile feature that allows you to load portions of a file into memory and modifications are buffered to disk for you. I also like the idea of writing to the end of the file and deleting the original row. It's not as if you have any indexing on record ids or anything...