Hi
Could anyone give me a simple code, codes that I could use to write data to a text file and then read the data. I a more detailed explaination I am creating a program that requires delphi to save information such as high scores "Permanently" so that when the program is closed and opened again the data is still there. I have read a few programs on the web but in my case i want the data on the text file to be from highest to lowest and not randomly added.
I hope this is detailed enough. Please feel free to contact me if you need pacifics. And sorry to anyone who as posted this in an other way before.
I would be of great help if anyone could assists me
What I would do is store the data in memory in a sorted form. Then you can write it easily. When you resave, simply wipe out the current data and rewrite it.
Reading the file should be straight-forward.Code:AssignFile(oFile,filename) rewrite(oFile) CloseFile(oFile) Append(oFile)
I am not totally clear on the method on how to do it. I am only a student and they consider this "more advanced". Could you please give an example of the methodology. At the moment I have my data added to A listbox and i smply want to right it to a file Permanently and then rewrite to the file everytime the list changes and that the file can be view when eg: A button is clicked. This would be very much appreciated.![]()
It seems that you have two different issues.
1) reading writing data. This is done with the readln/writeln functions.
2) keeping the data sorted in memory. This will depend on what data structures you are using.
Ive sort of got the hang woth using a text file, but could anyone help me to add new data without deleting the old data and to edit the old data by removing what i dont need. I also have the problem that my changes to my text file arent saved when i exit my program and restart it.
I have a edit box for my new high score (for the persons name) and labels to display the names of people with older high scores. (These names i want to edit without rewriting them which rewrites the file and leaves the new high score out) I have a button to confirm the data in the editbox and write it to the file. This is what my code looks like:
could anone tell me what i must add or change and under what sections in detail please.Code:unit assignment12_MDJ_u; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons; type TfrmHigh = class(TForm) ListBox1: TListBox; bmbVeiw: TBitBtn; bmb: TBitBtn; Label1: TLabel; Label2: TLabel; Label3: TLabel; Edit1: TEdit; BitBtn1: TBitBtn; procedure FormShow(Sender: TObject); procedure bmbClick(Sender: TObject); procedure bmbVeiwClick(Sender: TObject); procedure BitBtn1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var frmHigh: TfrmHigh; myfile : textfile; implementation uses assignment_MDJ_u; {$R *.dfm} procedure Write; var myfile : textfile; text : string; begin end; procedure TfrmHigh.FormShow(Sender: TObject); var text : string; begin end; procedure TfrmHigh.bmbClick(Sender: TObject); begin AssignFile(myFile, 'Test.txt'); ReWrite(myFile); WriteLn(myFile, EDIT1.text); WriteLn(myFile, label1.caption); WriteLn(myFile, label2.caption); WriteLn(myFile, label3.caption); CloseFile(myFile); end; procedure TfrmHigh.bmbVeiwClick(Sender: TObject); var text : string; begin Reset(myFile); while not Eof(myFile) do begin ReadLn(myFile, text); listbox1.Items.Add(text); end; CloseFile(myFile); end; end.
I hope im not too much of a burden but my text books dont tell me how to do this thanks.
Last edited by Jaan; 09-27-2008 at 06:11 AM. Reason: Use code tags when you're posting your codes!
You'll need to save your file in the OnFormClose event.
Don't try to work directly with the file contents when saving, just wipe and start over.
Hi I am now able to save my data to a File as you have seen above. I am using several pages in my program so i am using the show / hide methos to display them. My only problem is that when I but the code in the On show property and on hide property and run the program it gives me the following error (I/O 102) which means file not assigned. I tryed to write test data in the on activate property, it work, but i didnt want fixed data so I deleted it (still in Brackets below). This i though the data should be now saved but when I sstarted it up it gave me the problem as above. The on show property is the error when i used trace into. Could anyone please help me with this problem as still no text books help me and internet codes are not sutable.
Here is my code.
I hope this is not too much of a headach to anyone (My ignorance) but i hope this problem will be solved with great speed.Code:unit assignment12_MDJ_u; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, ExtCtrls; type TfrmHigh = class(TForm) edtNameinput: TEdit; bmbConfirm: TBitBtn; ListBox1: TListBox; imgBack: TImage; procedure FormShow(Sender: TObject); procedure FormHide(Sender: TObject); procedure FormActivate(Sender: TObject); procedure imgBackClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var frmHigh: TfrmHigh; myFile : TextFile; implementation uses assignment_MDJ_u; {$R *.dfm} procedure TfrmHigh.FormShow(Sender: TObject); var text : string; begin // Reopen the file for reading Reset(myFile); // Display the file contents while not Eof(myFile) do begin ReadLn(myFile, text); listbox1.Items.Add(text); end; // Close the file for the last time CloseFile(myFile); end; procedure TfrmHigh.FormHide(Sender: TObject); var text : string; begin // Try to open the Test.txt file for writing to AssignFile(myFile, 'Test.txt'); ReWrite(myFile); // Write to this file WriteLn(myFile, edtNameinput.text); WriteLn(myFile, listbox1.items.add(text)); // Close the file CloseFile(myFile); end; procedure TfrmHigh.FormActivate(Sender: TObject); var text : string; begin { // Try to open the Test.txt file for writing to AssignFile(myFile, 'Test.txt'); ReWrite(myFile); // Write words to this file WriteLn(myFile, '20 Sally'); WriteLn(myFile, '9 John'); WriteLn(myFile, '50 Jean'); WriteLn(myFile, '34 Sam'); // Close the file CloseFile(myFile);} end; procedure TfrmHigh.imgBackClick(Sender: TObject); begin frmHigh.Visible := False; frmWelcome.Visible := True; end; end.![]()
Last edited by WingedPanther; 09-29-2008 at 09:25 AM. Reason: added code tags.
You have to use the AssignFile command before you can do anything else.
Also, PLEASE use code tags (with the # button) when posting code.
Thanks it works well it was so simple, I'm sorry if I caused any annoyances. I will now remember to use code Tags![]()
Also you can use TStrings.LoadFromFile(), .SaveToFile(). It is simpler to parse, because you are loading whole stuff into the memory.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks