Can't write to a text file. lazarus / 13-General ---------------------------------------------------------------------- pcwright · 2006-11-03 19:18 Currently, I'm using Lazarus 9.16, running on FC5, 1G Intel CPU and 512Meg. RAM. I am trying to write a Quicken Interchange File (qif) from data downloaded from eBay as .csv and ported to a .dbf file. All goes well, iterating the table, the debugger (GNU) says the data is in the variable, the output file is created and closed but no data is written to the file.  I have tried write and writeln but still no data. I have tried type and untyped, tried the examples and these don't work either. Any help would be appreciated.  :roll: Thanks, Phil ---------------------------------------------------------------------- theo · 2006-11-03 20:17 Have you tried a TFileStream? http://community.freepascal.org:10000/docs- html/rtl/classes/tfilestream.html ---------------------------------------------------------------------- pcwright · 2006-11-08 03:35 Theo, Thanks for the boost. TFilestream works but the strings have four to five blanks at the beginning of each line using the following code: procedure TForm1.Button1Click(Sender: TObject); var  fstrm : TFileStream;  i : integer; begin       fstrm := TFileStream.Create('test.txt',fmCreate);       try        for i := 1 to 5 do begin        fstrm.WriteAnsiString('This is line ' + inttostr(i) + #13#10);        end; //End of for       finally        fstrm.free;       end;//end of try end; Nothing I tried, including trim and delete(s,1,4), will remove the blanks and GNUCash doesn't like them. Again any help will be appreciated. ---------------------------------------------------------------------- theo · 2006-11-08 09:57 I didn't even know there is a WriteAnsiString method in TStream. ;-) I do it the old fashioned way which should work: Code: [Select]procedure TForm1.Button1Click(Sender: TObject); var fstrm : TFileStream; i : integer; Wrt:AnsiString; begin fstrm := TFileStream.Create('/home/theo/lazatest.txt',fmCreate); try for i := 1 to 5 do begin Wrt:='This is line ' + inttostr(i) + #13#10; fstrm.Write(Pchar(Wrt)^ ,Length(Wrt)); end; //End of for finally fstrm.free; end;//end of try end; ---------------------------------------------------------------------- theo · 2006-11-08 10:22 Btw. this is imho a bug. Ansistring is null-terminated in my understanding, right? This procedure however writes a length indicator first, so it's at least wrong labeled. from streams.inc: Code: [Select]  Procedure TStream.WriteAnsiString (S : String);   Var L : Longint;   begin     L:=Length(S);     WriteBuffer (L,SizeOf(L));     WriteBuffer (Pointer(S)^,L);   end; ---------------------------------------------------------------------- Vincent Snijders · 2006-11-08 10:47 You are right that ansistrings are null terminated, i.e. after the last character of the string is a #0, it the string is longer than zero character. However, ansistrings may contain #0 charaters. So, if you want to read an ansistring from a stream, you cannot read the stream until you find the first #0 and conclude the ansistring is the string until the first #0. To be able to read ansistring, first the length is written, so that ansistrings can be read back. Conclusion: this is not a bug, but by design. BTW: to write a string to a stream without length, I would not cast it to PChar, but do it like this: Code: [Select]fstrm.Write(Wrt[1] ,Length(Wrt)); ---------------------------------------------------------------------- theo · 2006-11-08 10:54 Quote from: "Vincent" However, ansistrings may contain #0 charaters. So, if you want to read an ansistring from a stream, you cannot read the stream until you find the first #0 and conclude the ansistring is the string until the first #0. To be able to read ansistring, first the length is written, so that ansistrings can be read back. Conclusion: this is not a bug, but by design. I understand what you mean here. But I'm not sure if it's "good design" Wouldn't you use a stream then? At least one is confused about this method writing length indicators to a textfile. Quote from: "Vincent" BTW: to write a string to a stream without length, I would not cast it to PChar, but do it like this: Code: [Select]fstrm.Write(Wrt[1] ,Length(Wrt)); Why? Does it make a difference for the compiler? I'm interested! ---------------------------------------------------------------------- Vincent Snijders · 2006-11-08 10:59 Quote from: "theo" Quote from: "Vincent" BTW: to write a string to a stream without length, I would not cast it to PChar, but do it like this: Code: [Select]fstrm.Write(Wrt[1] ,Length(Wrt)); Why? Does it make a difference for the compiler? I'm interested! I don't know. Try your self. Compile your code with -al and inspect the generated assembler file (e.g. unit1.s). I suspect, my way is slightly faster. One thing I know, is that my method works also for shortstrings. So no code changes are required if I change from ansistrings to shortstring or reverse. ---------------------------------------------------------------------- Vincent Snijders · 2006-11-08 11:02 Quote from: "theo"Quote from: "Vincent" However, ansistrings may contain #0 charaters. So, if you want to read an ansistring from a stream, you cannot read the stream until you find the first #0 and conclude the ansistring is the string until the first #0. To be able to read ansistring, first the length is written, so that ansistrings can be read back. Conclusion: this is not a bug, but by design. I understand what you mean here. But I'm not sure if it's "good design" Wouldn't you use a stream then? At least one is confused about this method writing length indicators to a textfile. Now, you lost me. We are using a stream (viz. a TFileStream), aren't we? ---------------------------------------------------------------------- theo · 2006-11-08 11:19 QuoteNow, you lost me. We are using a stream (viz. a TFileStream), aren't we? You're right, I am confused. But obviously this method is for writing several AnsiStrings to a stream but these strings are written like they were binary data thus not relying on the #0 terminator. It's a bit confusing isn't it? ---------------------------------------------------------------------- pcwright · 2006-11-08 15:23 Theo, Thanks for the heads up. The code works and solves the problem. That does seem to me to be a bit more than is necessary. Is there any simpler way to do it or would I have to write a procedure? ---------------------------------------------------------------------- theo · 2006-11-08 17:39 Quote from: "pcwright" That does seem to me to be a bit more than is necessary. Is there any simpler way to do it or would I have to write a procedure? What's complicated about it? It's one line more than your code originally had. You could also use a TStringList though and "SaveToFile". ---------------------------------------------------------------------- Marc · 2006-11-09 10:53 prepending with a length byte/word/cardinal is common practice when writing variable length strings to a stream. Reading back such string is efficient since you know how much to read, so the string can be read at once. This is way faster than scanning chars one by one. ---------------------------------------------------------------------- theo · 2006-11-09 11:31 Quote from: "Marc"prepending with a length byte/word/cardinal is common practice when writing variable length strings to a stream. Reading back such string is efficient since you know how much to read, so the string can be read at once. This is way faster than scanning chars one by one. My point was never about the method per se, I just thought that it's name is a bit misleading. At least pcwright go it wrong and I had to lookup the source to understand what it's doing. I would expect a function TStream.WriteAnsiString do do this: Code: [Select]    WriteBuffer (Pointer(S)^,Length(S)); A function doing this: Code: [Select]    L:=Length(S);     WriteBuffer (L,SizeOf(L));     WriteBuffer (Pointer(S)^,L);     Should have a name like: WriteSizeAndString or WriteLengthAndString or WriteSizedAnsiString or MyEnglishIsNotGoodEnoughToFindAGoodName ;-) But it's not that important... ---------------------------------------------------------------------- Marc · 2006-11-09 15:25 NO, since ansistrings are per def variable in length it makes fully sense that WriteAnsiString writes the complete ansistring and not only its char contents (an AnsiString is more than only its chars) If you expect to write a buffer, write the buffer :-) ---------------------------------------------------------------------- theo · 2006-11-09 16:02 Of course you're right. I wasn't talking about the technical aspect that much. Only that pcwright got it wrong in the first place and me too, because we didn't expect it. btw: Here is a little piece of code I've just found on a NG which illustrates how an AnsiString is built up. Nice for playing ... ;-) Quoteprocedure GetHexDisplay(AData: Pointer; ASize: integer; AList: TStrings); var   i:          integer;   recLen:     integer;   tBuf:       PChar;   tLng:       integer;   theStream:  TMemoryStream;   tStr:       string;   tStrEnd:    string; begin   recLen := ASize;   AList.Add(EmptyStr);   theStream := TMemoryStream.Create;   try     theStream.Write(AData^, ASize);     theStream.Seek(0, soFromBeginning);     while (theStream.Position < theStream.Size) do     begin       if (recLen > (theStream.Size - theStream.Position)) then         recLen := theStream.Size - theStream.Position;       tBuf := AllocMem(recLen);       try         theStream.Read(tBuf[0], recLen);         tStrEnd := EmptyStr;         tStr    := EmptyStr;         for i := 0 to recLen - 1 do         begin           if ((i = 0) or ((i mod 16) = 0)) then           begin             if (i <> 0) then             begin               AList.Add(tStr + '|' + tStrEnd + '|');               tStrEnd := EmptyStr;             end;             tStr := Format('%5X', );             tStr := tStr + ': ';           end;           tStr := tStr + Format('%.02X ', [Byte(tBuf)]);           if (tBuf < char($20)) or (tBuf > char($7F)) then              tBuf := '.';           tStrEnd := tStrEnd + tBuf;         end;       finally         FreeMem(tBuf);       end;       if (tStrEnd <> EmptyStr) then       begin         if (Length(tStrEnd) < 16) then         begin           tLng := 16 - Length(tStrEnd);           while (tLng > 0) do           begin             tStr    := tStr + '   ';             tStrEnd := tStrEnd + ' ';             Dec(tLng, 1);           end;         end;         AList.Add(tStr+ '|' + tStrEnd + '|');         tStrEnd := EmptyStr;       end;     end;   finally     theStream.Free;   end;   if (tStrEnd <> EmptyStr) then   begin     if (Length(tStrEnd) < 16) then     begin       tLng := 16 - Length(tStrEnd);       while (tLng > 0) do       begin         tStr    := tStr    + '   ';         tStrEnd := tStrEnd + ' ';         Dec(tLng, 1);       end;     end;     AList.Add(tStr+ '|' + tStrEnd + '|');   end; end; procedure TForm1.Button1Click(Sender: TObject); var Str:AnsiString; begin  Memo1.Lines.Clear;  Str:='abcdef';  Str:=Str+'g';  GetHexDisplay(Pointer(PChar(Str) - 8), Length(Str)+8, Memo1.Lines); end;