[Done] ReadString
Moderator: Documentation Editors
[Done] ReadString
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.
In all examples where ReadString() is used, you need to add this function.
-
- Enthusiast
- Posts: 544
- Joined: Wed Sep 25, 2019 10:18 am
Re: ReadString
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
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
-
- Addict
- Posts: 4519
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: ReadString
Not necessarily, because there are also UTF-8 files without BOM.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.
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().
Yes.AZJIO wrote: In all examples where ReadString() is used, you need to add this function.
No, adding one more line will not make a simple code example too difficult.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?
And especially for new PureBasic users, it makes much sense to show them proper example code from the beginning.
Re: ReadString
It is better to make an example complex and correct than easy and incorrect.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?
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
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.
-
- Addict
- Posts: 4519
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: ReadString
So what? This has nothing to do with the text I wrote in my previous message. EOD.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.
-
- Enthusiast
- Posts: 544
- Joined: Wed Sep 25, 2019 10:18 am
Re: ReadString
@AZJIO
Creates a file in the #PB_Compiler_Home directory under Linux, but not in the Data subdirectory.file$ = #PB_Compiler_Home + "Examples\Sources\Data\test.pref"
If CreateFile(0, file$, #PB_UTF8)
Code: Select all
purebasic$ ls *.pref
'Examples\Sources\Data\test.pref'
- Andre
- PureBasic Team
- Posts: 2056
- Joined: Fri Apr 25, 2003 6:14 pm
- Location: Germany (Saxony, Deutscheinsiedel)
- Contact:
Re: ReadString
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?
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
-
- Addict
- Posts: 4519
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: ReadString
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
- Andre
- PureBasic Team
- Posts: 2056
- Joined: Fri Apr 25, 2003 6:14 pm
- Location: Germany (Saxony, Deutscheinsiedel)
- Contact:
Re: ReadString
Used this shorter example for the ReadFile() docs.
Thank you all
Thank you all