0

actually i was opened question previously,

but can't get answer what i exactly want, so i would like to ask again thanks All

for example, i have some text file name is 'test.txt' , and inside text contents looks like

hello all
good day
happy is

and i want to modify following source to iterate from first index of 'hello all' i mean..

if i click showmessage(first) then i want get 'hello' inside test.txt file,

and if click showmessage(second) then want to get 'all' and continuesly,

if i click again showmessage(first) then want to get 'good' and

click again showmessage(second) then want to get 'day' that what i want exactly.

Thanks in advance! and thanks all who helped me already!

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  list : TStringList;
  first, second, third: string;
begin
  list := TStringList.Create;
  try
    list.Delimiter := #32;
    list.LoadFromFile('test.txt');
    first := list[0];
    second := list[1];
    ShowMessage(first);
    ShowMessage(second);
  finally
    list.Free;
  end;
end;

Hello you can modify such like following ?

i want to use showmessage(first) and showmessage(two) , if so much appreciate!

procedure TForm1.BitBtn1Click(Sender: TObject);

var
  theFileStuff : tstringList;
  oneLine      : tStringList;
  x,y          : integer;
begin
    theFileStuff      := tStringList.Create;
    oneLine           := tStringList.create;
    oneLine.Delimiter := #32;
    theFileStuff.LoadFromFile('test.txt');
    for x := 0 to theFileStuff.count-1 do
    begin
          oneLine.DelimitedText := theFileStuff[x];
          for y := 0 to oneLine.count-1
          do
          //ShowMessage(oneLine[y]);
          ShowMessage(first);
          ShowMessage(second);

    end;
    oneLine.Free;
    theFileStuff.Free;
end;
2
  • 2
    @Paul: Surely you can think of a way to change "ShowMessage(oneline[y]);" into "first := oneline[0]; second := oneline[1]; ShowMessage(first); ShowMessage(second);". You need to try to figure things out yourself and not expect everyone to do everything for you. You'll never learn that way. Commented Dec 18, 2009 at 21:26
  • Hello thanks all! i was resolved,if without other's help maybe i couldn't resolve it i really appreciate ! thanks again :) Commented Dec 18, 2009 at 22:09

2 Answers 2

4

Try this

procedure TForm1.ShowFields(Sender: TObject);
var
  theFileStuff : tstringList;
  oneLine      : tStringList;
  x,y          : integer;
begin
    theFileStuff      := tStringList.Create;
    oneLine           := tStringList.create;
    oneLine.Delimiter := #32;
    theFileStuff.LoadFromFile('fileName');
    for x := 0 to theFileStuff.count-1 do
    begin
          oneLine.DelimitedText := theFileStuff[x];
          for y := 0 to oneLine.count-1
          do ShowMessage(oneLine[y]);
    end;
    oneLine.Free;
    theFileStuff.Free;
end;

If you know there are only two items per line, you can replace the following code:

for y := 0 to oneLine.count-1
do ShowMessage(oneLine[y])

with

ShowMessage(oneLine[0]);    // First
ShowMessage(oneLine[1]);    // Second

My code was more generic to handle any number of items per line

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your help! i was modified some would you check it ? thanks again
2

The Delimiter property only has meaning when using the DelimitedText property. You will have to use 2 separate TStringList objects for what you are asking for, for example:

var
  list, values : TStringList;
  curListIdx, curValueIdx: Integer;

procedure TForm1.FormCreate(Sender: TObject);
begin
  curListIdx := -1;
  curValueIdx := -1;
  list := TStringList.Create; 
  values := TStringList.Create;
  values.Delimiter := #32;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  list.Free;
  values.Free;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  S: String;
begin 
  if curListIdx = -1 then
  begin
    list.LoadFromFile('test.txt');
    if list.Count = 0 then Exit;
    curListIdx := 0;
  end;

  if curValueIdx = -1 then
  begin
    if curListIdx = list.Count then
    begin
      curListIdx := -1;
      Exit;
    end;
    values.DelimitedText := list[curListIdx];
    Inc(curListIdx);
    if values.Count = 0 then Exit;
    curValueIdx := 0;
  end;

  S := values[curValueIdx]; 
  Inc(curValueIdx)
  if curValueIdx = values.Count then curValueIdx := -1;

  ShowMessage(S);
end;

2 Comments

Hello Thanks for your help! i want to modify such like,,would you help me
Remy, you can do it with one TStringList (SL) and a string variable. SL.LoadFromFile(), assign SL.Text to the variable, and then assign the variable back into SL.DelimitedText after setting the SL.Delimiter (and probably SL.StrictDelimiter := True).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.