Archive: Reading from a file


Reading from a file
I'm having trouble reading from a file. I need to read a string from a file and then set it to a text field. I have no problems doing the text field but reading the file is causing me serious problems.

Here is a small section of the file I need to read:

"SteamAppData"
{
"RememberPassword" "1"
"AutoLoginUser" "anaddress@somewhere.com"
"anotheraddress@nowhere.com"
{
"User" "anaddress@somewhere.com"
"AccountCreated" "1"
"ShowNotifyTrayHintDialog" "0"
"ShowExitDialog" "1"
"ShowExitLogoutDialog" "1"


I need to read the "AutoLoginUser" line. i.e I need to set "anaddress@somewhere.com" to a variable.

Any help would be greatly appreciated.


Thanks for reading.


Was my question too hard? Did I not give enough info? Or has it been answered else where?


You can use FileOpen to open the file for reading. Then you can read every line using FileRead.

If it's a line you don't want to change (check the line using string function like StrCmp and Cpy), add the line to a variable, otherwise add the changed value.

Then close the file, open it for writing and write everything back.


Thanks for the help

I do not wish to modify the file at all, just open, read, and close.

However how do I capture one part of the line. Is there a StringTokenizer or anything like that I need to use?


That's even easier. There is a StrTok function available in the StrFunc header file.


thanks, great help


Sorry to be a pain but the StrFunc wont work. I've done

!include "StrFunc.nsh"

But when I try to use any of the methods it just gives me errors. I even went as far as using the examples and they still don't work.

Section

${StrStr} $3 "This is just an example" "just"

SectionEnd


I get this error:
Section: ""
!insertmacro: FUNCTION_STRING_StrStr
!insertmacro: macro "FUNCTION_STRING_StrStr" requires 0 parameter(s), passed 3!
Error in script "stdin" on line 99 -- aborting creation process


What am I doing wrong?


I apologize for the tripple post but I've come up with a different way of doing it (since I couldn't get StrFunc working. I did the following:

FileOpen $1 $INSTDIR\config\SteamAppData.vdf r
FileRead $1 $2
FileRead $1 $2
FileRead $1 $2
FileRead $1 $2

StrCpy $7 $2 80 19

FileClose $1


It almost works. $7 = anaddress@somewhere.com"

but I need the " to be taken of the end. Is this possible? Or is it still better to use StrFunc?


They work fine, you only have to initialize them first (see StrFunc.txt).

You can trim a trailing character using StrLen and StrCpy.


Oops I forgot to initialize them. Thanks for that I'll remember it for next time.

However in the mean time I worked something else out. It might not be elegant but it works

FileOpen $1 $INSTDIR\config\SteamAppData.vdf r
FileRead $1 $2
FileRead $1 $2
FileRead $1 $2
FileRead $1 $2

StrCpy $7 $2 80 19

loop:
IntOp $8 $8 + 1
StrCpy $9 $7 $8
StrCmp $7 $9 trimIt loop

trimIt:
IntOp $8 $8 - 2
StrCpy $6 $7 $8

FileClose $1



Thanks for all your help