We help IT Professionals succeed at work.

apply a break in memo1

thisguirong
thisguirong asked
on
hi i would like to record memo1.lines into an ini file, however i can't seen to record all the text if there are breaks in memo1. Please advise me how can i do this.
Comment
Watch Question

Commented:
If you mean Empty Lines (breaks) in a Memo then just test the line for charaters first

if not (Memo1.Lines[5] = '') then
  WriteToIni;

I don't think you can write nothing (empty line) to an INI.
CERTIFIED EXPERT
Top Expert 2004

Commented:
? memo1.lines.savetofile('whatever.ini');
How about something like this:

var
  Count: Integer;
  MyIni: TIniFile;
begin
  MyIni := TIniFile.Create('MEMOFILE.INI');
  try
    for Count := 0 to Memo1.Lines.Count -1 do
      MyIni.WriteString('Section', IntToStr(Count), Memo1.Lines[Count]));
  finally
    MyIni.Free;
  end;
end;

Then, to load the the contents back into the Memo:

var
  Count: Integer;
  MyIni: TIniFile;
  Values: TStringList;
begin
  MyIni := TIniFile.Create('MEMOFILE.INI');
  Values := TStringList.Create;
  try
    MyIni.ReadSection('Section', Values);
    Memo1.Lines.Clear;
    for Count := 0 to Values.Count -1 do
      Memo1.Lines.Add(MyIni.ReadString('Section', Values[Count], '');
  finally
    MyIni.Free;
    Values.Free;
  end;
end;


I hope this helps you.

Cheers,

Stu
CERTIFIED EXPERT
Top Expert 2014

Commented:
You can open the file and perform your own I/O.  If you do this, you'll be responsible for creating the section headers and key=value line format.

Author

Commented:
To Slick812:

thanks, but problem about breaks is still there :P

To kretzschmar:

Its working~ however i wish to enter many records, thus using the ini file, and my computer always crash easily if there are many ini files around, so.. the break problem is solved, but another problem enters :P

To Stuart Johnson and everyone:

Wow its working, thanks~ you're showing me how to use the sections in an ini file . However Do you think its possible to record into ini files without using sections by converting breaks into a code like \n in C programming, then when read from ini, converts \n into breaks? i will add more points :P

To aikimark:

open which file? i'm lost : P
Hi thisguirong,

I'm not sure what you mean here, but I'll have a shot at it.

You have a memo containing this information:


Humpty Dumpty Sat On The Wall

Humpty Dumpty Had A Great Fall

All The Kings Horses And All The Kings Men

Said F**k him, He's Only An Egg.


What you want to do is convert this to:

Humpty Dumpty Sat On The Wall
/n
Humpty Dumpty Had A Great Fall
/n
All The Kings Horses And All The Kings Men
/n
Said F**k him, He's Only An Egg.


So, your file will look like:
Humpty Dumpty Sat On The Wall
/n
Humpty Dumpty Had A Great Fall
/n
All The Kings Horses And All The Kings Men
/n
Said F**k him, He's Only An Egg.


Is that what you mean?  If not, please explain it with a bit more detail (an example would be great)

Stu
Further:

If this is what you are doing, then you are defeating the whole purpose of the IniFile.  Instead you should be looking at plain old ASCII text files.

procedure SaveMemo(Memo: TMemo);
var
  sList: TStringList;
  Count: Integer;
begin
  sList := TStringList.Create;
  try
    sList.Assign(Memo.Lines);
    for Count := 0 to sList.Count -1 do
      begin
        if sList[Count] = '' then
          sList[Count] := '/n';
        {if you want to add the /n to the end of each line, uncomment this}
//        sList[Count] := sList[Count] + '/n';
      end;
    sList.SaveToFile('C:\Out.txt');
  finally
    sList.free;
  end;
end;

I can go trickier if you want.  But at this stage, I'd prefer to find out what you want this file to look like.

Stu

Author

Commented:
I mean record the following:

"ask not what you can
 do for expert exchange
 ask what expert exchange
 can do for you"

into the ini file:

"ask not what you can/ndo for expert exchange/nask what expert exchange/n can do for you"

this is my new /n break idea, the program converts /n if breaks are detected, and converts back in memo1. however it does not match the question of applying breaks, its about changing strings. Slick812 mention its not possible to record breaks, and you show me how to do it by saving as sections, thus i award points for your answer :D

Thank you everyone~ have a nice day~ f*** the eggs
CERTIFIED EXPERT
Top Expert 2014

Commented:
output to the .INI file.  In Stuart Johnson comment dated 01/14/2002 03:24AM, he shows you an standard I/O example.  His code output to a .TXT file.

I don't know what the contents of your .INI file look like, nor what your user can do to screw it up in the memo field.  Do you need to do any validation?

Author

Commented:
thanks aikimark~ :D
everything's fine now

Explore More ContentExplore courses, solutions, and other research materials related to this topic.