tags:

views:

121

answers:

3

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.

A: 

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

vodkhang
I liked your idea. Could you please write some code for me how to do it in array? It will be great help as I am struggling in programming.
rahat
you mean how to load data from a flat file to an array? or how do you update your array?
vodkhang
i mean both. How to update the array and than add the data back into file.
rahat
You have an array of objects, right? Then you can loop and find the object, easy? You can just overwrite the old file with all the new data from the array
vodkhang
thanks for your help. But could you please put me on right track. I am from networking background and this is first time doing some programing. Could you please atleast write small code for me. It will be a great help for me.Cheers
rahat
"Plz send teh codez" is kinda discouraged…
aehiilrs
A: 

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.

Jamie Altizer
A: 

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...

EchoCoder
Maybe this suggestion is a bit advanced?
ChaosPandion