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
Saving and reading a Text file
Started by Sparky, Sep 26 2008 09:16 AM
11 replies to this topic
#1
Posted 26 September 2008 - 09:16 AM
|
|
|
#2
Posted 26 September 2008 - 10:31 AM
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.
AssignFile(oFile,filename) rewrite(oFile) CloseFile(oFile) Append(oFile)
Reading the file should be straight-forward.
#3
Posted 26 September 2008 - 11:31 AM
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.:)
#4
Posted 26 September 2008 - 01:36 PM
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.
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.
#5
Posted 27 September 2008 - 04:20 AM
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.
I hope im not too much of a burden but my text books dont tell me how to do this thanks.
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:
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.
could anone tell me what i must add or change and under what sections in detail please.
I hope im not too much of a burden but my text books dont tell me how to do this thanks.
Edited by Jaan, 27 September 2008 - 05:11 AM.
Use code tags when you're posting your codes!
#6
Posted 27 September 2008 - 10:24 AM
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.
Don't try to work directly with the file contents when saving, just wipe and start over.
#7
Posted 27 September 2008 - 11:43 PM
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.
Here is my 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
[COLOR="Lime"] {[/COLOR] // 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);[COLOR="Lime"]}[/COLOR]
end;
procedure TfrmHigh.imgBackClick(Sender: TObject);
begin
frmHigh.Visible := False;
frmWelcome.Visible := True;
end;
end.
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.:)
Edited by WingedPanther, 29 September 2008 - 08:25 AM.
added code tags.
#8
Posted 29 September 2008 - 08:25 AM
You have to use the AssignFile command before you can do anything else.
Also, PLEASE use code tags (with the # button) when posting code.
Also, PLEASE use code tags (with the # button) when posting code.
#9
Posted 29 September 2008 - 10:23 PM
Thanks it works well it was so simple, I'm sorry if I caused any annoyances. I will now remember to use code Tags:)
#10
Posted 18 November 2008 - 09:25 AM
Also you can use TStrings.LoadFromFile(), .SaveToFile(). It is simpler to parse, because you are loading whole stuff into the memory.
#11
Posted 26 November 2008 - 12:14 AM
Reading the whole text file into memory is good when the text file isn't too big. Otherwise it will have to be read line by line.
Patrick Ooi, http://www.webhyper.com
#12
Posted 03 December 2008 - 08:27 AM
When you work with AssignFile it's good to make this test
AssignFile(xx,xx);
If NoT FileExists(xx) Then
Rewrite(xx); //You make sure that you wont try to recreate the file, and make sure that the file always gonna be there! [=
AssignFile(xx,xx);
If NoT FileExists(xx) Then
Rewrite(xx); //You make sure that you wont try to recreate the file, and make sure that the file always gonna be there! [=


Sign In
Create Account


Back to top









