Now, finally, my question. Is this a bug or is that an "undocumented Feature" ? If the latter, I'll document it on eg http://wiki.freepascal.org/File_Handling_In_Pascal (http://wiki.freepascal.org/File_Handling_In_Pascal)Neither, its the result of untyped constants and misunderstanding what the modern string variable holds. You can think of a string as a pointer to an array of characters. The variable
...uninformed user would expect.Maybe uninformed users would take a look at the wiki...
Hi taazz, thanks for that. Yes, aware of how it happens now but my point is its not what an uninformed user would expect. The function accepts a string, other functions that accept a string know not to use the first char (string) or the management block (ANSIstring).No it does not accept a string, it accepts a buffer any buffer. If it was of type string you would not be able to pass anything but strings.
I sure agree that changing the behaviour is totally off the agenda but am asking why I should not document what I found. I spent, maybe, 2 hours looking at this, I'd like to prevent other people from making the same assumptions.It is valuable to have the behavior documented as a caution for the new comers I agree, the exact wording or presentation of the behavior I leave it to more experienced members.
But new enough to want to show my plan to the more experienced before editing the wiki ....
So, streams been there since Delphi 2 ? I used only Delphi 1, long time ago !
Thanks mate !
David
You could use the filestream methods dedicated to strings, and not worry about the file format itself.Have you looked at those method your self? they inject the length of the string infront of every string written. that might be acceptable and might not.
Maybe uninformed users would take a look at the wiki...
It takes seconds to find several examples with str[1]... :P
You could use the filestream methods dedicated to strings, and not worry about the file format itself.
e.g.
fs.WriteAnsiString(S1);
You could use the filestream methods dedicated to strings, and not worry about the file format itself.
e.g.
fs.WriteAnsiString(S1);
Actually howardpc, I tried that before posting. It does not produce a clean ascii text file. Did you try it yourself ? Be interesting to know if you cat'ed the file at the console or relied on your programme being able to re-read it ? I suspect some (eg) editors, trying to be helpful, filter out some things they think, quite rightly, don't belong.
My comment to not worry about the file format itself was intended to indicate that TFileStream ReadAnsiString/WriteAnsiString are not designed to create traditional .txt files (structured only by CR/LF) but binary files that can be read and written consistently using streaming, just as Readln/Writeln can be used for reading and writing traditional .txt files using classic Pascal routines. Their advantage is the increased speed of reading this gives since extra string length information is stored in the file (so does not have to be calculated by parsing as the file is read as with a .txt file). The disadvantage is the exotic format, and the slightly larger file size.
Ah, I see what you mean. Might suit some others but certainly not me. I'm writing out some xml to be read in another application so must be plain old text file I'm afraid.If you are writing xml, just use a TFilestream and write buffered, do not use the string functions in that case.