Board index » delphi » Reading null-terminated strings from files
SylV
![]() Delphi Developer |
Tue, 14 Jun 2005 18:46:47 GMT
|
SylV
![]() Delphi Developer |
Tue, 14 Jun 2005 18:46:47 GMT
Reading null-terminated strings from files
Hello
I would like to read null-terminated strings from a file, in binary mode. I don't know their lengths, I only know that the last byte of each string is 0. How can I do that? I want to avoid this method: - read byte per byte until finding 0 - calculate the length of the string - read the string because it's quite slow. Thanks |
Peter Schulthei
![]() Delphi Developer |
Tue, 14 Jun 2005 23:42:25 GMT
Re:Reading null-terminated strings from files"SylV" <syl...@club-internet.fr> schrieb im Newsbeitrag Quote> Hello A maybe usable solution 1.) Open a Handle to the file with CreateFile Season greetings BTW: Realnames are highly appriciated in this group Diese Mitteilung besteht zu 99,5% aus recycleten Bytes. |
J Fren
![]() Delphi Developer |
Wed, 15 Jun 2005 01:07:36 GMT
Re:Reading null-terminated strings from filesOn Fri, 27 Dec 2002 16:42:25 +0100, "Peter Schultheis" Quote<peterschulth...@5t1n2t.1t> wrote: Quote
Var // Now the Data is in Buffer Start := 1; The last string needs to be handled, that can be done by making Buffer |
SylV
![]() Delphi Developer |
Fri, 17 Jun 2005 02:40:48 GMT
Re:Reading null-terminated strings from filesThanks for your answers. It's a pity that TStringList.DelmimitedText doesn't work with #0 as Delimiter... |
AlanGLLo
![]() Delphi Developer |
Fri, 17 Jun 2005 04:07:07 GMT
Re:Reading null-terminated strings from filesIn article <3e0c2f1c$0$7360$7a628...@news.club-internet.fr>, "SylV" Quote<syl...@club-internet.fr> writes: you can do (untested) ... var Alan Lloyd |
Crutch
![]() Delphi Developer |
Sat, 18 Jun 2005 15:05:39 GMT
Re:Reading null-terminated strings from filesI'm assuming that you aren't using a carriage return to define each line - you are using Chr(0). A solution might be to just read the entire file into a string - you will var The way you solve this problem depends of it you want to load the file fast I would prefer the latter method (set up string array). Hope this helps. |
Chris Willi
![]() Delphi Developer |
Sun, 19 Jun 2005 00:40:25 GMT
Re:Reading null-terminated strings from filesLoading the file into a string/buffer and changing #0 to #13 and then loading into a stringlist will break the lines at #13, but it will also break the lines at #10, #13, and #13#10. To get just the #0 terminated strings somewhere, somplace, you have to read byte by byte to find the #0s. Following takes about 10sec to get all #0 terminated strings out of the .............. // reads from current stream position xlen := 0; Inc(i); if len < 0 then begin bEOF := red = 0; if len > 0 then begin Stream.Seek(Offset, soFromBeginning); end; procedure TForm1.Button8Click(Sender: TObject); Strs := TStringList.Create; Memo1.Lines.Assign( Strs ); .............. QuoteSylV wrote: |
Kelly Leah
![]() Delphi Developer |
Mon, 20 Jun 2005 04:31:50 GMT
Re:Reading null-terminated strings from filesif your file size is too large to read into a string, and if you're using WinNT or one of it's decendents (XP, 2000), you can use Memory Mapped files (see CreateFileMapping and MapViewOfFile). these may work on some other OSes (95, 98, etc.), but I'm not sure and I Kelly |
Maarten Wiltin
![]() Delphi Developer |
Mon, 20 Jun 2005 06:27:06 GMT
Re:Reading null-terminated strings from filesQuoteKelly Leahy wrote in message ... ... Yeah, right. If your file is larger than 2 GB, you have other problems. And if anybody ever designed a video format where the Groetjes, |
Kelly Leah
![]() Delphi Developer |
Mon, 20 Jun 2005 09:31:21 GMT
Re:Reading null-terminated strings from filesNot everyone wants to allocate a large block of memory just for the purpose of reading in the contents of a file (I'd rather not have to allocate a 500MB buffer just because I'm looking for a few strings in a file that big). Mapping a view is an easy way to let the operating system decide whether the Kelly |
AlanGLLo
![]() Delphi Developer |
Mon, 20 Jun 2005 14:25:56 GMT
Re:Reading null-terminated strings from filesIn article <JDMQ9.57$yJ6.50052...@newssvr12.news.prodigy.com>, "Kelly Leahy" Quote<kellyle...@nospam.swbell.net> writes: Alan Lloyd |
Kelly Leah
![]() Delphi Developer |
Tue, 21 Jun 2005 00:50:46 GMT
Re:Reading null-terminated strings from filesIt doesn't know as much if you allocate the memory yourself. For instance, you can use a flag to tell windows that you're going to go forward only through the file, so it will swap out the earlier pages once you've loaded the next page from the file. It doesn't have quite as much information when you use something like GlobalAlloc (or other similar allocation methods that are based on the heap). An example of another interesting effect: consider an allocation method that 1) How long does it take windows to zero the memory? I think you can see my point. Kelly |