Link to home
Start Free TrialLog in
Avatar of OneNeutrino
OneNeutrinoFlag for United States of America

asked on

Having Issue with my code

I wrote a piece of code to parse a widestring for anything between quotation marks and output it to 3 different memo boxes and then to a txt file. It works but a reason that I can't place, its duplicating the first 2 of the 3 fields in both cases (the memo boxes and the txt file). So for example, the output would look like this:

Box1 | Box 2           | Box 3
IP#1 | LoginName#1 | NickName#1
IP#1 | LoginName#1 | NickName#2
IP#2 | LoginName#2 | NickName#3
IP#2 | LoginName#2 | NickName#4

The same output gets sent to the text file and I can't figure it out.

I am not a very experienced programmer so go easy :), but here is the code:

AssignFile(myFile, 'C:\Logs\Log' + LogNum.Text + '.txt');
  myString := objSocket.ReceiveString();
  len := Length(myString);
  IPs.Text := '';
  User.Text := '';
  LoginM.Text := '';
  i := 0;
  count := 0;
  while i < len do
  begin
    if myString[i] = '"' then
    begin
      outString := '';
      i := i + 1;
      while myString[i] <> '"' do
      begin
        outString := outString + myString[i];
        i := i + 1;
      end;
      count := count + 1;
    end;
    case count of
      1 :
        begin
          IPs.Lines.Add(outString);
          fullString := fullString + outString + ' ';
        end;
      2 :
        begin
          User.Lines.Add(outString);
          fullString := fullString + outString + ' ';
        end;
      3 :
        begin
          LoginM.Lines.Add(outString);
          fullString := fullString + outString;
          Append(myFile);
          WriteLn(myFile, fullString);
          CloseFile(myFile);
          fullString := '';
          count := 0;
        end;
    end;
    i := i + 1;
  end;
Avatar of Mike Littlewood
Mike Littlewood
Flag of United Kingdom of Great Britain and Northern Ireland image

Can you give an example of the starting string that you recieve.
Avatar of Limbeck
Limbeck

does the string you recieve look like ?

"aa","bb","cc"
"aa","bb","cc"
"aa","bb","cc"

if so, try changing this :

  if myString[i] = '"' then
    begin
      outString := '';
      i := i + 1;
      while myString[i] <> '"' do
      begin
        outString := outString + myString[i];
        i := i + 1;
        if (i<len) then
          if mystring[i]=',' then
            i:=i+1;

      end;
      count := count + 1;
    end;
Avatar of OneNeutrino

ASKER

The string recieved could be anything from:

   abcdefg 345 sfjklsjk 6435 klj23jk4l 1243 "dsfjklds" "fjkskldsjf"  djfkljsdfkl "fjkdsfjlds"

to that plus 8 more lines similar to it. Each line has 3 sets of quotation marks and tons of other data. Each time a string is received, it could be 1 line or 15 lines, or more. The wide string is updated with a fresh receive every 1 second.
hi, try changing this

    begin
        outString := outString + myString[i];
   //     i := i + 1;
      end;
If I did that it wouldn't step through the string and would be a runaway loop.
Anyone have any other ideas?
Avatar of dinilud
procedure TForm1.Button1Click(Sender: TObject);
var i,len,count:Integer;
    outString,fullString:String;
begin
  myString:='"dsfjklds" "fjkskldsjf" "fjkdsfjlds"';
  len := Length(myString);
  IPs.Text := '';
  User.Text := '';
  LoginM.Text := '';
  Memo1.Text:='';
  i := 0;
  count := 0;
  while i < len do
  begin
    if myString[i] = '"' then
    begin
      outString := '';
      i := i + 1;
      while myString[i] <> '"' do
      begin
        outString := outString + myString[i];
        i := i + 1;
      end;
      count := count + 1;

      case count of
      1 :
        begin
          IPs.Lines.Add(outString);
          fullString := fullString + outString + ' ';
        end;
      2 :
        begin
          User.Lines.Add(outString);
          fullString := fullString + outString + ' ';
        end;
      3 :
        begin
          LoginM.Lines.Add(outString);
          fullString := fullString + outString;
          Memo1.Lines.Add(fullString);
          fullString := '';
          count := 0;
        end;
      end;
    end;
    i := i + 1;
  end;
end;
ASKER CERTIFIED SOLUTION
Avatar of dinilud
dinilud
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks man! Its amazing how moving one little 'end' can fix everything, but then that figures...