Richard Holland
Richard Holland

Reputation: 2683

Delphi 2010 WriteLn to TextFile cutting off around 55 characters

Given this mess (designed years ago to write some data out csv "field1","field2") the output file has began stopping around 55 characters. Same happens in a showmessage() call or placing inside a local String variable. Converting to a TStringList to clean the code up even fails in the same way. Inspecting this as a local string variable in the IDE while the code is stopped shows the entire built string as planned. ShowMessage() even puts trailing ellipsis ... on the end.

I end up with "LD","BC63781S","JACKSON","MS","DENVER","CO","1186","0"... in showmessage and "LD","BC63781S","JACKSON","MS","DENVER","CO","1186","0" in the on disk file.

Better readability https://gist.github.com/788839

Writeln(F,
  '"'+ ACtion
  + '","' + Ini.ReadString('IP_ITS','BAccount','TEST')
  + '","' + FieldByName('PICKCITY').AsString
  + '","' + FieldByName('PICKST').AsString
  + '","' + FieldByName('DROPCITY').AsString
  + '","' + FieldByName('DROPST').AsString
  + '","' + FieldByName('TOT_MILES').AsString
  + '","' + FloatToStr(AWeight)
  + '","' + FieldByName('LENGTH').AsString
  + '","' + FloatToStr(AStops)
  + '","' + ''{grosspay}
  + '","' + FieldByName('PICK_DATE').AsString
  + '","' + FieldByName('PICK_TIME').AsString
  + '","' + FieldByName('DROP_DATE').AsString
  + '","' + FieldByName('DROP_TIME').AsString
  + '","' + AEquip
  + '","","' + ALTL
  + '"' + ',"","' + '","1","'
  + Ini.ReadString('IP_ITS','BComp','BAccount')
  + FieldByName('PRO_NO').AsString
  + '","","","","",""'
  )

Upvotes: 0

Views: 1420

Answers (4)

Rafael Colucci
Rafael Colucci

Reputation: 6078

Your text file may contains invalid characters like #0 (end of a string). Consider a string like this:

sString := 'This a string #0 with a huge problem'.

If you do a ShowMessage(sString), it will only display "This a string" because a #0 indicates a end of string character.

Remeber that you cant actually see the #0 char, you may need to create a procedure to search for this char and delete it.

A better solution would be to not use TStringList at all, neither use Wite(). Use a Stream descendent, like TFileStream to get rid of this problem for good.

Upvotes: -1

Arnaud Bouchez
Arnaud Bouchez

Reputation: 43033

Two fix possitibilies:

1) Instead of the + sign between each string, just use a , instead. It will let Writeln() do the text concatenation.

          Writeln(F, '"',ACtion,'","', Ini.ReadString('IP_ITS','BAccount','TEST'),'","',
            FieldByName('PICKCITY').AsString,'","',FieldByName('PICKST').AsString,'","',
            FieldByName('DROPCITY').AsString,'","',FieldByName('DROPST').AsString,'","',
            FieldByName('TOT_MILES').AsString,'","',FloatToStr(AWeight),","',
            FieldByName('LENGTH').AsString,'","',FloatToStr(AStops),
            '","',''{grosspay},'","',FieldByName('PICK_DATE').AsString,
            '","',FieldByName('PICK_TIME').AsString,'","',FieldByName('DROP_DATE').AsString,
            '","',FieldByName('DROP_TIME').AsString,'","',AEquip,
            '","","',ALTL,'"' , ',"","',
            '","1","',Ini.ReadString('IP_ITS','BComp','BAccount'),FieldByName('PRO_NO').AsString,
            '","","","","",""');

2) Use a Format() then an open parameter array - this is my preferred way, because it will be also more easy to fix/maintain

  Writeln(F, format('"%s","%s","%s","%s","%s","%s","%s",  ....
    [ACtion,Ini.ReadString('IP_ITS','BAccount','TEST'),FieldByName('PICKCITY').AsString, ..... ]));

And in all cases, make sure you've set a writing buffer of some size, and are using {$I-}:

procedure TClassData.SaveToFile(const FileName: TFileName);
var F: system.Text;
    buf: array[word] of byte;
begin
  {$I-}
  assign(F,FileName);
  system.SetTextBuf(F,buf);
  rewrite(F);
  if ioresult=0 then
  begin
    writeln(F,...
    ...

Upvotes: 5

Kromster
Kromster

Reputation: 7397

Did you enabled "huge strings", "range checking" and "overflow checking" in Project > Options > Compiler ? Delete all your projects DCU files, and force project rebuild. That may help.

Upvotes: 1

Richard Holland
Richard Holland

Reputation: 2683

For reasons I may never understand the act of observing the string data forced it to truncate. ShowMessage() and no stops it writes the file out fine.

Put a ShowMessage() or stop and no dice.

Quantum Delphi?

Perhaps a Flush call would have helped by why would ShowMessage() cut off at 55 chars?

Upvotes: 1

Related Questions