none
VFP9 Variables From File RRS feed

  • Question

  • Hello VFP users,

    I'm trying to use the content of a txt file and use it as variables in a program.

    client.txt
    CLIENT="Client Name"
    PROJECT="Project Name"

    I would like the CLIENT, PROJECT to become a variables in my PRG holding what ever comes after the equal sign.

    I found the following code: Currently it's reading and storing as a line. How do I change this?

    LOCAL lnFileHandle && numeric file handle
    lnFileHandle = FOPEN( "j:\70722\raw\ctlclient")
    IF lnFileHandle = -1
       && error, could not open the file
       && do something to handle the error
       RETURN
    ENDIF
    LOCAL lcLine && define a variable to hold each line
    DO WHILE NOT FEOF( lnFileHandle) && loop through the file
       lcLine = FGETS( lnFileHandle) && store each line in lcLine
       ?lcLine && do something with the line, such as display it
    ENDDO

    Thank you for your help with this.

    Thursday, July 4, 2019 4:29 PM

Answers

  • Hello,

    try this

    cfile = "E:\client.txt"
    cclient = ""
    cpro = ""

    nanz = ALINES(acx,FILETOSTR(cfile))
    FOR nschl = 1 TO nanz
     ct = acx[nschl]                 && better readable
     IF !EMPTY(AT("CLIENT=",ct))       && left(ct,7)="CLIENT="
      cclient = GETWORDNUM(ct,2,"=")  && = substr(ct,8)
     ENDIF
     IF !EMPTY(AT("PROJECT=",ct))     && left(ct,7)="PROJECT="
      cpro = GETWORDNUM(ct,2,"=")   && = substr(ct,9)
     ENDIF
    NEXT nschl

    ? cclient
    ? cPro

    You could also add [DATA] as first line to file and use api-functions iniread and iniwrite

    Best regards

    tom

    Friday, July 5, 2019 7:24 AM
  • I agree with Tom Knauf that ALINES() is better here, but I assume you want to turn whatever's in the file into variables. You can use STREXTRACT() for that. Inside the loop through the array created by ALINES(), do something like this:

    cVarName=STREXTRACT(acx[nschl],'','=')
    cValue = STREXTRACT(acx[nschl],'=')
    LOCAL &cVarName
    STORE m.cValue to (m.cVarName

    That said, I'm not sure how variables whose names you don't know are useful to you.

    Tamar

    Friday, July 5, 2019 8:24 PM
    Moderator

All replies

  • Hello,

    try this

    cfile = "E:\client.txt"
    cclient = ""
    cpro = ""

    nanz = ALINES(acx,FILETOSTR(cfile))
    FOR nschl = 1 TO nanz
     ct = acx[nschl]                 && better readable
     IF !EMPTY(AT("CLIENT=",ct))       && left(ct,7)="CLIENT="
      cclient = GETWORDNUM(ct,2,"=")  && = substr(ct,8)
     ENDIF
     IF !EMPTY(AT("PROJECT=",ct))     && left(ct,7)="PROJECT="
      cpro = GETWORDNUM(ct,2,"=")   && = substr(ct,9)
     ENDIF
    NEXT nschl

    ? cclient
    ? cPro

    You could also add [DATA] as first line to file and use api-functions iniread and iniwrite

    Best regards

    tom

    Friday, July 5, 2019 7:24 AM
  • I agree with Tom Knauf that ALINES() is better here, but I assume you want to turn whatever's in the file into variables. You can use STREXTRACT() for that. Inside the loop through the array created by ALINES(), do something like this:

    cVarName=STREXTRACT(acx[nschl],'','=')
    cValue = STREXTRACT(acx[nschl],'=')
    LOCAL &cVarName
    STORE m.cValue to (m.cVarName

    That said, I'm not sure how variables whose names you don't know are useful to you.

    Tamar

    Friday, July 5, 2019 8:24 PM
    Moderator