Search code examples
powerbuilder

How many rows of data in a text file


I'm new to Powerbuilder and here I have some question about how to find how many rows/lines of data there is in a text file. First I tried a approach of FileLength but could not figure how to to do that with my current logic. Could not find any example of how to do it with it either so here I am wanting some help!

What I'm doing is generating a password from a txt file.

Notes: gsa_wordlist is a global variable.

Also if you as experienced developer see any common pitfalls then please do tell.

My code on Click():

int li_rand
int li_upperboundList
int li_FileNum
string ls_fileurl
string ls_listout
int i
li_upperboundList = Upperbound(gsa_wordlist)
ls_fileurl = 'C:\Users\abg\Documents\wordlist.txt'

IF li_upperboundList < 100 THEN
Beep(1)

li_FileNum = FileOpen(ls_fileurl)
FOR i = 1 TO // TO HOW MANY LINES OF DATA
    FileReadEx(li_FileNum, ls_listout)
    gsa_wordlist[i] = ls_listout
NEXT

li_upperboundList = Upperbound(gsa_wordlist)
li_rand = rand(li_upperboundList)
sle_genpass.Text = string(li_rand)
ELSE
    Beep(2)
    li_rand = rand(li_upperboundList)
    sle_genpass.Text = gsa_wordlist[li_rand]
END IF

Solution

  • You don't need to know how many rows, just loop until EOF.

    li_FileNum=FileOpen(ls_fileurl,LineMode!,Read!,LockRead!)
    
    i = 1
    
    DO While FileReadEx(li_FileNum,ls_listout) <> -100
       gsa_wordlist[i++] = ls_listout
    LOOP
    FileClose(li_FileNum)
    

    PS. Add a check inside the loop for the li_upperboundList limit !