Readstring. Is it an error or just an empty string?

Just starting out? Need help? Post your questions and find answers here.
Pud
User
User
Posts: 28
Joined: Mon Dec 28, 2009 2:23 am

Readstring. Is it an error or just an empty string?

Post by Pud »

"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
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Readstring. Is it an error or just an empty string?

Post by Saki »

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
地球上の平和
BarryG
Addict
Addict
Posts: 3324
Joined: Thu Apr 18, 2019 8:17 am

Re: Readstring. Is it an error or just an empty string?

Post by BarryG »

That's not the answer, Saki. The text file could look like this:

Code: Select all

one
two
three

four

five

six
seven
Any of those blank lines will return an empty string with ReadString(), thus indicating an error.
Pud
User
User
Posts: 28
Joined: Mon Dec 28, 2009 2:23 am

Re: Readstring. Is it an error or just an empty string?

Post by Pud »

Sorry my bad I didn’t mean just one string but a series of CR/LF delimited records.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Readstring. Is it an error or just an empty string?

Post by Saki »

Hi @Barry
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.
地球上の平和
User avatar
NicTheQuick
Addict
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?

Post by NicTheQuick »

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.
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.
Pud
User
User
Posts: 28
Joined: Mon Dec 28, 2009 2:23 am

Re: Readstring. Is it an error or just an empty string?

Post by Pud »

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.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Readstring. Is it an error or just an empty string?

Post by Saki »

Hi Nick, yes that works not, an inadvertence...
地球上の平和
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Readstring. Is it an error or just an empty string?

Post by RASHAD »

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
User avatar
NicTheQuick
Addict
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?

Post by NicTheQuick »

RASHAD wrote: Mon Jun 28, 2021 1:10 pm 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
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.
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.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Readstring. Is it an error or just an empty string?

Post by RASHAD »

Sorry NicTheQuick
I did not read you previous post as I was busy listening to Irish Music :mrgreen:
Egypt my love
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Readstring. Is it an error or just an empty string?

Post by Saki »

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

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
Image
Last edited by Saki on Tue Jun 29, 2021 9:47 am, edited 2 times in total.
地球上の平和
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Readstring. Is it an error or just an empty string?

Post by Keya »

Saki btw what does BF stand for? been wondering for weeks lol :)
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Readstring. Is it an error or just an empty string?

Post by Saki »

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.
地球上の平和
Post Reply