Recent

Author Topic: quirks in writing text to file  (Read 2876 times)

daringly

  • Jr. Member
  • **
  • Posts: 73
quirks in writing text to file
« on: October 08, 2023, 12:16:59 am »
These two codes give very different output. There is a weird character in teststring that disables output to the textfile, and it looks like '' or ", not sure which.

assign(fila);
rewrite(fila);
writeln(fila,teststring);
close(fila);

vs
writeln(teststring);

These two codes give very different output. There is a weird character in teststring that disables output to the textfile. The second time you write this code, writing to the file works normally.

If I look at the string's first character in debug, I get
pchar: 77 'M' String: 34 '\"'

How can I identify and remove this escape code from the string so that the visual and file output are similar?

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12398
  • Debugger - SynEdit - and more
    • wiki
Re: quirks in writing text to file
« Reply #1 on: October 08, 2023, 12:58:20 am »

If I look at the string's first character in debug, I get
pchar: 77 'M' String: 34 '\"'

About the debuggers output.

Depending on various things, the fpc compiler does not give the debugger enough info if your variable is a pchar, or a string.
However if you watch something like "MyText[1]" in a string "[1]" means the first char. A pchar however starts at index 0, sot "[1]" is the 2nd char. => the debugger shows both.


For the rest I am not sure, 77 would be M
32 would be "  (not sure why it is shown with a \ before)

I don't know why they affect your file. Have you looked at it in a hex editor?

Also why do you only look at the FIRST char in the debugger? Maybe the issues is a later char?



daringly

  • Jr. Member
  • **
  • Posts: 73
Re: quirks in writing text to file
« Reply #2 on: October 08, 2023, 01:10:30 am »
Thanks.

I looked at 0 and 1. Everything after the first 2 characters is "normal" compared to other strings that work normally.
With string[0], pchar is 34.

When I removed that first character (using rightstr, skipping first character) the text file worked correctly.

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: quirks in writing text to file
« Reply #3 on: October 08, 2023, 08:49:41 am »
When I removed that first character (using rightstr, skipping first character) the text file worked correctly.
In which case that can only lead to one conclusion and that is that the 'wrong' character was already present in your teststring otherwise it would not have ended up in your file. It could be the 'wrong' character isn't printable in the terminal (e.g. presentable by a visible ascii/utf character).

Anything else without compilable code that is capable of reproducing the error is just pure speculation.

Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   sysutils;
  7.  
  8. procedure WriteTestString(teststring: string);
  9. var
  10.   fd: textfile;
  11. begin
  12.   // write to terminal
  13.   WriteLn(teststring);
  14.  
  15.   // write to file
  16.   AssignFile(fd, 'some_test_file.txt');
  17.  
  18.   {$push}
  19.   {$I+}
  20.   try
  21.     Rewrite(fd);
  22.     writeln(fd, teststring);
  23.     CloseFile(fd);
  24.   except
  25.     on E: EInOutError
  26.       do writeln('ERROR: error occurred during file operation: ', E.ClassName, ' / ', E.Message);
  27.   end;
  28.   {$pop}
  29. end;
  30.  
  31. begin
  32.   WriteTestString('"This is a test string."');
  33. end.
  34.  
Today is tomorrow's yesterday.

 

TinyPortal © 2005-2018