Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Saving and reading a Text file

  1. #1
    Sparky's Avatar
    Sparky is offline Newbie
    Join Date
    Sep 2008
    Posts
    10
    Rep Power
    0

    Saving and reading a Text file

    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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Saving and reading a Text file

    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.
    Code:
    AssignFile(oFile,filename)
    rewrite(oFile)
    CloseFile(oFile)
    Append(oFile)
    Reading the file should be straight-forward.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Sparky's Avatar
    Sparky is offline Newbie
    Join Date
    Sep 2008
    Posts
    10
    Rep Power
    0

    Re: Saving and reading a Text file

    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.

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Saving and reading a Text file

    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.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    Sparky's Avatar
    Sparky is offline Newbie
    Join Date
    Sep 2008
    Posts
    10
    Rep Power
    0

    Re: Saving and reading a Text file

    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:

    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.
    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.
    Last edited by Jaan; 09-27-2008 at 06:11 AM. Reason: Use code tags when you're posting your codes!

  7. #6
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Saving and reading a Text file

    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.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  8. #7
    Sparky's Avatar
    Sparky is offline Newbie
    Join Date
    Sep 2008
    Posts
    10
    Rep Power
    0

    Re: Saving and reading a Text file

    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.
    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.
    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.
    Last edited by WingedPanther; 09-29-2008 at 09:25 AM. Reason: added code tags.

  9. #8
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Saving and reading a Text file

    You have to use the AssignFile command before you can do anything else.

    Also, PLEASE use code tags (with the # button) when posting code.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  10. #9
    Sparky's Avatar
    Sparky is offline Newbie
    Join Date
    Sep 2008
    Posts
    10
    Rep Power
    0

    Re: Saving and reading a Text file

    Thanks it works well it was so simple, I'm sorry if I caused any annoyances. I will now remember to use code Tags

  11. #10
    lucuis is offline Newbie
    Join Date
    Nov 2008
    Posts
    21
    Rep Power
    0

    Re: Saving and reading a Text file

    Also you can use TStrings.LoadFromFile(), .SaveToFile(). It is simpler to parse, because you are loading whole stuff into the memory.

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Reading a text file and saving to settings.
    By Blimp in forum Visual Basic Programming
    Replies: 7
    Last Post: 09-17-2010, 06:42 AM
  2. Saving to Text file question
    By X_Programmer in forum C# Programming
    Replies: 6
    Last Post: 08-30-2010, 12:13 PM
  3. Saving from com port to text file help!!
    By cs342 in forum C and C++
    Replies: 10
    Last Post: 05-24-2010, 06:54 AM
  4. reading a text file
    By deelicious in forum Java Help
    Replies: 5
    Last Post: 05-20-2010, 08:31 PM
  5. C# reading in a text file.
    By recoded in forum C# Programming
    Replies: 2
    Last Post: 04-01-2008, 07:50 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts