views:

626

answers:

2

Hello, All

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;
+3  A: 

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

Sparky
thanks for your help! i was modified some would you check it ? thanks again
paul
@Paul: Check it yourself. You can't learn without doing.
Ken White
Hello thanks again! :) i was resolved it now !
paul
+2  A: 

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;
Remy Lebeau - TeamB
Hello Thanks for your help! i want to modify such like,,would you help me
paul
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).
Ken White