[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 159
  • Last Modified:

Text file save & edit !

I need the folowing code that will have 1 memo in which i will be able to load txt file or to write somthing to memo and save it to some file, or if that file dosen't exsist create file and save it.
0
65zgtre45rr
Asked:
65zgtre45rr
  • 3
  • 2
  • 2
4 Solutions
 
2266180Commented:

that is pretty simple:

memo1.lines.savetofile('the filename with or without path that wil be created or overwritten');
for loading:
memo1.lines.loadfromfile('filename');
0
 
D-MasterCommented:
to load a file in a memo1
memo1.lines.loadfromfile('example.txt');

to save memo1 to a file
memo2.lines.savetofile('example.txt');
0
 
D-MasterCommented:
sorry didn't see ur comment ciuly
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
2266180Commented:
well we posted kind of at the same time, you were just a few seconds slower :)
0
 
Scay7Commented:
To add onto what ciuly and D-Master said :P

The "savetofile" and "loadfromfile" file should do what you need but incase you needed something else:

var
  Form1: TForm1;
  filename : string;
  f : textfile;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);   //save to file.
begin
  if not fileexists(filename) then
  begin
    assignfile(f,filename);
    rewrite(f);
    closefile(f);
  end;
  memo1.Lines.SaveToFile(filename);
end;
procedure TForm1.Button2Click(Sender: TObject);  //load from file with check.
begin
if fileexists(filename) then memo1.Lines.LoadFromFile(filename)
                        else showmessage('Sorry that file doesn''t exist...');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
filename := 'c:\test.txt';
end;


Peace Scay7
0
 
2266180Commented:
there is no need for the:
  if not fileexists(filename) then
  begin
    assignfile(f,filename);
    rewrite(f);
    closefile(f);
  end;
part. the savetofile will overwrite the file if it exists (at least in delphi 7). :)
0
 
Scay7Commented:
Ye i know thats why i said heres another way, mabey he wants to do something else
who knows small peice of code wont harm anything and can be expanded to do something
else if need be.
So i added it...  :PP ciuly

Peace Scay7
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now