Board index » delphi » Should be a simple question...

Should be a simple question...

I'm doing a simple program with delphi 2.
I have a memo component on a form, and the idea is that when the program
starts the memo shows the contents of a text file associated with it.
The user should be able to amend the text in the memo, and then when you
close the program the text file gets overwritten with the new version.
The problem is that whenever the program is closed and then opened again
without altering the text the cursor in the memo drops down a line, so
that after a few times you have the end of the text and then a big gap
with no text before the cursor appears.
the file is read to the memo with
        While not Eof(NoteFile) do
        begin
                Readln(noteFile, Chunk);
                Memo1.Lines.Add(Chunk);
        end;
where NoteFile is the text file and Chunk is a string,
and saved to file with
        begin
                ReWrite(NoteFile);
                Writeln(NoteFile,Memo1.Text);
                CloseFile(NoteFile);
        end;
Any ideas, anyone? I get the feeling it ought to be really easy.....
Regards,
--
Fast Bert

 

Re:Should be a simple question...


On Wed, 26 Feb 1997 22:04:50 +0000, Fast Bert

Quote
<Fast_B...@thenest.demon.co.uk> wrote:
>I'm doing a simple program with delphi 2.
>I have a memo component on a form, and the idea is that when the program
>starts the memo shows the contents of a text file associated with it.
>The user should be able to amend the text in the memo, and then when you
>close the program the text file gets overwritten with the new version.
>The problem is that whenever the program is closed and then opened again
>without altering the text the cursor in the memo drops down a line, so
>that after a few times you have the end of the text and then a big gap
>with no text before the cursor appears.
>the file is read to the memo with
>        While not Eof(NoteFile) do
>        begin
>                Readln(noteFile, Chunk);
>                Memo1.Lines.Add(Chunk);
>        end;
>where NoteFile is the text file and Chunk is a string,
>and saved to file with
>        begin
>                ReWrite(NoteFile);
>                Writeln(NoteFile,Memo1.Text);
>                CloseFile(NoteFile);
>        end;

try reading the text directly:

MyMemo.Lines.LoadFromFile (filename);
MyMemo.modified := FALSE;

before closing, save only if modified

if MyMemo.Modified then MyMemo.Lines.SaveToFile (filename)
;

jeff lawrence
department of biological sciences
university of pittsburg
jlawre...@vms.cis.pitt.edu

Re:Should be a simple question...


Quote
Fast Bert wrote:

> I'm doing a simple program with delphi 2.
> I have a memo component on a form, and the idea is that when the program
> starts the memo shows the contents of a text file associated with it.
> The user should be able to amend the text in the memo, and then when you
> close the program the text file gets overwritten with the new version.
> The problem is that whenever the program is closed and then opened again
> without altering the text the cursor in the memo drops down a line, so
> that after a few times you have the end of the text and then a big gap
> with no text before the cursor appears.
> the file is read to the memo with
>         While not Eof(NoteFile) do
>         begin
>                 Readln(noteFile, Chunk);
>                 Memo1.Lines.Add(Chunk);
>         end;
> where NoteFile is the text file and Chunk is a string,
> and saved to file with
>         begin
>                 ReWrite(NoteFile);
>                 Writeln(NoteFile,Memo1.Text);
>                 CloseFile(NoteFile);
>         end;
> Any ideas, anyone? I get the feeling it ought to be really easy.....
> Regards,
> --
> Fast Bert

My recollection of WriteLn is that it writes a string and appends a
newline, and possibly this is causing a problem.  Write, if I recal
correctly doesn't add a newline.  However, you can avoid the WriteLn by
using the LoadFromStream/SaveToStream methods.  If you open a file
stream ( TFileStream ), instead of a file, then you can use the
LoadFromStream method of a memo box's lines property to load the whole
thing at once.  Likewise, you can save the contents of a memo box's
lines back to a file stream with SaveToStream.  The only trick to this
is that you will need to set the position property in the stream from
which you are copying to 0, as this is not automatically done for you by
LoadFromStream.

Re:Should be a simple question...


In article <3315B988.4...@switchsolutions.com>, Stuart
<sste...@switchsolutions.com> writes
       <<<<<<<<<original description snipped>>>>>>>>>>>>>>>>>

Quote
>> Any ideas, anyone? I get the feeling it ought to be really easy.....
>> Regards,
>> --
>> Fast Bert

>My recollection of WriteLn is that it writes a string and appends a
>newline, and possibly this is causing a problem.  Write, if I recal
>correctly doesn't add a newline.  However, you can avoid the WriteLn by
>using the LoadFromStream/SaveToStream methods.  If you open a file
>stream ( TFileStream ), instead of a file, then you can use the
>LoadFromStream method of a memo box's lines property to load the whole
>thing at once.  Likewise, you can save the contents of a memo box's
>lines back to a file stream with SaveToStream.  The only trick to this
>is that you will need to set the position property in the stream from
>which you are copying to 0, as this is not automatically done for you by
>LoadFromStream.

Thanks very much to all who replied - I have now fixed the problem by
changing the Writeln instruction to Write. It seems that Writeln must
indeed append a new line, and Write not.
Regards,
--
Fast Bert

Re:Should be a simple question...


Hello Fast!

Wednesday February 26 1997, Fast Bert wrote to All:

 FB> without altering the text the cursor in the memo drops down a line, so
 FB> that after a few times you have the end of the text and then a big gap
Load:
Memo1.Lines.LoadFromFile('Filename');

Save:
Memo1.Lines.SaveToFile('FileName');

Kind regards Ole

    E-Mail: Ole_Morten...@Hotmail.com

Other Threads