Readstring. Is it an error or just an empty string?
Readstring. Is it an error or just an empty string?
"Return value
Returns the read string, or an empty string if the read has failed."
What if your file contains empty strings how can you detect an error?
Cheers
Returns the read string, or an empty string if the read has failed."
What if your file contains empty strings how can you detect an error?
Cheers
Re: Readstring. Is it an error or just an empty string?
Code: Select all
If FileSize(path$)<1 : Debug "Empty or not readable as File" : Endif
Code: Select all
If not LOF(file) : Debug "Empty" : endif
地球上の平和
Re: Readstring. Is it an error or just an empty string?
That's not the answer, Saki. The text file could look like this:
Any of those blank lines will return an empty string with ReadString(), thus indicating an error.
Code: Select all
one
two
three
four
five
six
seven
Re: Readstring. Is it an error or just an empty string?
Sorry my bad I didn’t mean just one string but a series of CR/LF delimited records.
Re: Readstring. Is it an error or just an empty string?
Hi @Barry
Ah, if I understand correctly, then he must use the flag
#PB_File_IgnoreEOL.
Ah, if I understand correctly, then he must use the flag
#PB_File_IgnoreEOL.
Code: Select all
; As sample so :
string$=ReadString(file, #PB_File_IgnoreEOL|#PB_UTF8)
Last edited by Saki on Mon Jun 28, 2021 1:01 pm, edited 3 times in total.
地球上の平和
- NicTheQuick
- Addict
- Posts: 1227
- Joined: Sun Jun 22, 2003 7:43 pm
- Location: Germany, Saarbrücken
- Contact:
Re: Readstring. Is it an error or just an empty string?
You can check if Loc(file) has changed after ReadString. If it has increased then you just read an empty string.
@Saki: Also you can not use #PB_Any with ReadString.
@Saki: Also you can not use #PB_Any with ReadString.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Re: Readstring. Is it an error or just an empty string?
Saki, no ignore EOL is not a solution as I read many records separated with an EOL. See BarryG’s example above.
NicTheQuick. I’ll experiment with that one, cheers.
NicTheQuick. I’ll experiment with that one, cheers.
Re: Readstring. Is it an error or just an empty string?
Hi Nick, yes that works not, an inadvertence...
地球上の平和
Re: Readstring. Is it an error or just an empty string?
I am not sure but it worth try
Code: Select all
If ReadFile(0, GetTemporaryDirectory()+"Test.txt") ; if the file could be read, we continue...
While Eof(0) = 0 ; loop as long the 'end of file' isn't reached
Text$ = ReadString(0) ; display line by line in the debug window
Position.q = Loc(0)
If Text$ = "" And Position = oldposition.q+2
Debug "Empty Line"
ElseIf Text$ = "" And Position > oldposition.q+2
Debug "Line Error"
EndIf
oldposition.q = Position.q
Wend
CloseFile(0) ; close the previously opened file
Else
MessageRequester("Information","Couldn't open the file!")
EndIf
Egypt my love
- NicTheQuick
- Addict
- Posts: 1227
- Joined: Sun Jun 22, 2003 7:43 pm
- Location: Germany, Saarbrücken
- Contact:
Re: Readstring. Is it an error or just an empty string?
Comparing with the old position +2 is not a good idea. If you have a plain ASCII file and Windows line endings (#CRLF$) this would work. But it does not work with UTF-16 files or with Linux line endings (#LF$) encoded as ASCII or UTF-8.RASHAD wrote: ↑Mon Jun 28, 2021 1:10 pm I am not sure but it worth tryCode: Select all
If ReadFile(0, GetTemporaryDirectory()+"Test.txt") ; if the file could be read, we continue... While Eof(0) = 0 ; loop as long the 'end of file' isn't reached Text$ = ReadString(0) ; display line by line in the debug window Position.q = Loc(0) If Text$ = "" And Position = oldposition.q+2 Debug "Empty Line" ElseIf Text$ = "" And Position > oldposition.q+2 Debug "Line Error" EndIf oldposition.q = Position.q Wend CloseFile(0) ; close the previously opened file Else MessageRequester("Information","Couldn't open the file!") EndIf
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Re: Readstring. Is it an error or just an empty string?
Sorry NicTheQuick
I did not read you previous post as I was busy listening to Irish Music
I did not read you previous post as I was busy listening to Irish Music
Egypt my love
Re: Readstring. Is it an error or just an empty string?
Hi @Pud
You can do it also with StringFields_BF, i think.
viewtopic.php?f=12&t=77219
The code for the small text file above then looks like this, multi OS
You can do it also with StringFields_BF, i think.
viewtopic.php?f=12&t=77219
The code for the small text file above then looks like this, multi OS
Code: Select all
Define index, separator$=#LF$
Define file=ReadFile(#PB_Any, "C:\Users\albus\OneDrive\Desktop\text.txt")
Define text$=ReadString(file, #PB_File_IgnoreEOL|#PB_UTF8)
If FindString(text$, #CR$) ; A part from DrawText_BF
text$=ReplaceString(text$, #CRLF$, #LF$)
text$=ReplaceString(text$, #LFCR$, #LF$)
ReplaceString(text$, #CR$, #LF$, #PB_String_InPlace)
EndIf
Define mode=0
Debug "Mode : "+mode
StringFields_BF(text$, separator$, mode)
Debug "String length : "+Len(text$)
Debug "StringFields found : "+GetAmountStringFields_BF()
Debug "Empty StringFields found : "+GetEmptyStringFields_BF()
Debug "=============================="
For index=1 To GetAmountStringFields_BF()
Debug GetStringFields_BF(index)
Next
FreeAllStringFields_BF() ; Free the actual cached StringField list and init for a new function call
Last edited by Saki on Tue Jun 29, 2021 9:47 am, edited 2 times in total.
地球上の平和
Re: Readstring. Is it an error or just an empty string?
Saki btw what does BF stand for? been wondering for weeks lol
Re: Readstring. Is it an error or just an empty string?
BF = Coders "Best Friend" - LOL
The Wizzard core code was once created from a BucketFill routine
which then became BucketFill_advanced and finally GFX_Wizzard_BF.
It was then kept to be able to assign the codes to a common source.
The Wizzard core code was once created from a BucketFill routine
which then became BucketFill_advanced and finally GFX_Wizzard_BF.
It was then kept to be able to assign the codes to a common source.
地球上の平和