When i move the "pwriter->Close();" code to the btnExit()
and move the "StreamWriter^ pwriter = gcnew StreamWriter("memberFile.dat");" code inside the formload(), My application saves multiple records like i would like it to.
But.. THE PROBLEM IS:
I would like the program to do the following when the btnSave is clicked:
Open the file.
Write data to the file. <-- below the data that is already there (Not wipe the file and +new data)
Close the file.
I want my file to contain the 1st members details and then the 2nd members details below the 1st and 3rd below the second ect..
See StreamWriter.StreamWriter(String, Boolean) Constructor:
Determines whether data is to be appended to the file. If the file exists and append is false, the file is overwritten. If the file exists and append is true, the data is appended to the file. Otherwise, a new file is created.
the code you gave me:
"StreamWriter^ pwriter = gcnew StreamWriter("memberFile.dat", true);"
appends the data to the end of the file which is exactly what i needed!
my file now adds members sequencially and looks like this:
12 chilcote close, croydon, surrey
2 <--- line 6 in the memberFile.dat (start of member 2)
72 chargrill road, kent <--- line 10 (end of member 2's details)
and i can append further records without any problem.
The next problem i have now found is updating:
Now i would like to be able to update record 2, i already have already set up a members array and memberList array which holds the members as objects when they are typed in. This allows me to search for member 2 and bring up (in this case "danniella's" details) and display them in the text boxes (btnFind Code shown below).
I guess i could turn append to false and rewrite the whole file sequencially going through the array member by member and appending them to the file.. but the drawback is obviously that with this method the whole file will be re-written when only 1 record needs updating, this isnt a problem for small amounts of data, but when theres thousands of members this method (an update) could become time consuming.. there must be a better way..
Im not sure if this is possible but i was thinking..
possibly rewrite values at lines 6 to 12 in the text file (memberFile.dat) with the values brought up from the search for member "2" after clicking "btnFind" then "btnSave".
**oh and i already have an "if", "else" function using the array to determine whether the record is already existing (an update) or a new record.
here is the code from my btnFind incase anyone is interested:
This is restriction of text files. Updating means rewriting the whole file. You may read all text file to a list of class instances, and edit them in memory. In the end, save the whole file.
Or consider using some other format, like XML or database.