[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 158
  • Last Modified:

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;
0
OneNeutrino
Asked:
OneNeutrino
  • 4
  • 2
  • 2
  • +1
1 Solution
 
mikelittlewoodCommented:
Can you give an example of the starting string that you recieve.
0
 
LimbeckCommented:
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;
0
 
OneNeutrinoAuthor Commented:
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LimbeckCommented:
hi, try changing this

    begin
        outString := outString + myString[i];
   //     i := i + 1;
      end;
0
 
OneNeutrinoAuthor Commented:
If I did that it wouldn't step through the string and would be a runaway loop.
0
 
OneNeutrinoAuthor Commented:
Anyone have any other ideas?
0
 
diniludCommented:
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;
0
 
diniludCommented:
 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;

      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;  //end of case
    end;  // end of "if myString[i] = '"' then "
    i := i + 1;
  end;  //end of while
0
 
OneNeutrinoAuthor Commented:
Thanks man! Its amazing how moving one little 'end' can fix everything, but then that figures...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now