[Done] ReadString

Found an issue in the documentation ? Please report it here !

Moderator: Documentation Editors

AZJIO
Addict
Addict
Posts: 1318
Joined: Sun May 14, 2017 1:48 am

[Done] ReadString

Post by AZJIO »

Specify in the help file to require ReadStringFormat() before using ReadString() to place a pointer at the end of the BOM label and read the given files. If this is not done, then when reading UTF-8, one extra character from the BOM label will end up at the beginning of the line.

In all examples where ReadString() is used, you need to add this function.
juergenkulow
Enthusiast
Enthusiast
Posts: 544
Joined: Wed Sep 25, 2019 10:18 am

Re: ReadString

Post by juergenkulow »

I'm not sure if this addition makes the example for the first program with files too difficult?
github Documentation/English/File.txt Line 569

Code: Select all

  If ReadFile(0, "Text.txt")   ; if the file could be read, we continue...
    ReadStringFormat(0)        ; Read over text file labeling, if available.
    While Eof(0) = 0           ; loop as long the 'end of file' isn't reached
      Debug ReadString(0)      ; display line by line in the debug window
    Wend
    CloseFile(0)               ; close the previously opened file
  Else
    MessageRequester("Information","Couldn't open the file!")
  EndIf
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: ReadString

Post by Little John »

AZJIO wrote: If this is not done, then when reading UTF-8, one extra character from the BOM label will end up at the beginning of the line.
Not necessarily, because there are also UTF-8 files without BOM.
However, when reading an unknown UTF-8 file, ReadStringFormat() should always be used, as we cannot know in advance whether the file has a BOM or not. And if the file does not have a BOM, there is no harm in using ReadStringFormat().
AZJIO wrote: In all examples where ReadString() is used, you need to add this function.
Yes.
juergenkulow wrote: Fri Nov 03, 2023 7:39 am I'm not sure if this addition makes the example for the first program with files too difficult?
No, adding one more line will not make a simple code example too difficult.
And especially for new PureBasic users, it makes much sense to show them proper example code from the beginning.
AZJIO
Addict
Addict
Posts: 1318
Joined: Sun May 14, 2017 1:48 am

Re: ReadString

Post by AZJIO »

juergenkulow wrote: Fri Nov 03, 2023 7:39 am I'm not sure if this addition makes the example for the first program with files too difficult?
It is better to make an example complex and correct than easy and incorrect.

Code: Select all

EnableExplicit
Define file$, Format
file$ = #PB_Compiler_Home + "Examples\Sources\Data\test.txt"
If CreateFile(0, file$, #PB_UTF8)
	WriteString(0, "String")
	CloseFile(0)
Else
	MessageRequester("Information","may not create the file!")
	End
EndIf

If ReadFile(0, file$)
	Format = ReadStringFormat(0)
	Debug ReadString(0, Format + #PB_File_IgnoreEOL)
	CloseFile(0)
Else
	MessageRequester("Information", "Couldn't open the file!")
EndIf
It is necessary that the sample file be present in the "Data" folder, so as not to create it by complicating the example with unnecessary functions

Little John
When reading files of a multilingual program, UTF-8+BOM was used, and if there was a ";" character at the beginning of the line. then the line was ignored, and if it was in the first line, then the line was not ignored. It turned out that the symbol ";" was the second, and the first was the character from the BOM marker. We assumed that ReadStringFormat() would move the pointer to the correct position. And it worked.
Last edited by AZJIO on Sat Nov 04, 2023 1:42 pm, edited 1 time in total.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: ReadString

Post by Little John »

AZJIO wrote: Fri Nov 03, 2023 10:16 am Little John
When reading files of a multilingual program, UTF-8+BOM was used, and if there was a ";" character at the beginning of the line. then the line was ignored, and if it was in the first line, then the line was not ignored. It turned out that the symbol ";" was the second, and the first was the character from the BOM marker. We assumed that ReadStringFormat() would move the pointer to the correct position. And it worked.
So what? This has nothing to do with the text I wrote in my previous message. EOD.
juergenkulow
Enthusiast
Enthusiast
Posts: 544
Joined: Wed Sep 25, 2019 10:18 am

Re: ReadString

Post by juergenkulow »

@AZJIO
file$ = #PB_Compiler_Home + "Examples\Sources\Data\test.pref"
If CreateFile(0, file$, #PB_UTF8)
Creates a file in the #PB_Compiler_Home directory under Linux, but not in the Data subdirectory.

Code: Select all

purebasic$ ls *.pref 
'Examples\Sources\Data\test.pref'
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2056
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: ReadString

Post by Andre »

I thought about adding adding this slightly modified example at the ReadString documents for an 'Example using the format and #PB_File_IgnoreEOL flags'.

But I'm not sure, if it's the best solution... any suggestions for an additional 'Remark' to add?

Code: Select all

EnableExplicit
Define file$, Format
file$ = GetTemporaryDirectory() + "test.txt"
If CreateFile(0, file$, #PB_UTF8)
  WriteString(0, "String")
  CloseFile(0)
Else
  MessageRequester("Error", "File couldn't be created!")
  End
EndIf

If ReadFile(0, file$)
  Format = ReadStringFormat(0)
  Debug ReadString(0, Format | #PB_File_IgnoreEOL)
  CloseFile(0)
Else
  MessageRequester("Information", "File couldn't be opened!")
EndIf
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: ReadString

Post by Little John »

Just make a small addition to the example that currently exists in the help in the ReadFile() topic:

Code: Select all

If ReadFile(0, "Text.txt")         ; if the file could be read, we continue ...
   Format = ReadStringFormat(0)
   While Eof(0) = 0                ; loop as long the 'end of file' isn't reached
      Debug ReadString(0, Format)  ; display line by line in the debug window
   Wend
   CloseFile(0)                    ; close the previously opened file
Else
   MessageRequester("Information", "Couldn't open the file!")
EndIf
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2056
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: ReadString

Post by Andre »

Used this shorter example for the ReadFile() docs.
Thank you all :)
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
Post Reply