Archive: Read Multiline Text from a file - What am I done wrong?


Read Multiline Text from a file - What am I done wrong?
What I try to do is to read the file content (text < 1KB) contained escape-sequenced string & to display it a message-box.

Anyone has any idea why its not working? (I see the text as-is, & not in 3 lines)

---

The code:

FileOpen $1 "$INSTDIR\uninstall-warning.txt" r ; open file
FileRead $1 $2 ; we read until the end of line (including carriage return and new line) and save it to $1
FileClose $1 ; and close the file
StrCmp $2 "" +3 0 ; jump +3 if equal (jump 0 if not equal)
MessageBox MB_YESNO $2 IDYES continue
Quit


File Content:

IMPORTANT WARNING!$\nBy uninstalling you may be contravening your employer's Acceptable Use Policy.$\nPlease consider carefully whether you have the appropriate authority to take this action.$\n


You're expecting something to replace the three character newline ( $\n ) with a single ANSI newline. Neither FileRead nor MessageBox will do that for you. You can do it with WordReplace though.

!include "WordFunc.nsh"
... open and read the file
StrCpy $R0 $
StrCpy $R0 "$R0\n"
${WordReplace} $2 $R0 $\n + $2
... then call MessageBox
The StrCpy statements are needed to generate the newline search term as three characters instead of a single character. (The WordReplace won't find the single character newline, but it will find the three character newline).

Or just store the text in the file with actual new-lines.

Stu


demiller9,
It works - Thanks!

---

Afrow UK,
I checked your idea:

I.e. File Content:

IMPORTANT WARNING!\nBy uninstalling you may be contravening your employer's Acceptable Use Policy.\nPlease consider carefully whether you have the appropriate authority to take this action.\n

Does not work for me.

---

I wonder,
Is it possible representing multiline-text in the file itself, eliminating the need of WordReplace?


I don't think you understand. $\n and \n are just ASCII representations of new-lines in NSIS and C respectively which are translated to real new-line characters at compile time. If you want to read new-lines from the text file then actually insert new-line characters (i.e. press the enter key!) You'll then have to read all the lines in a loop and concatenate them in a variable.

Stu


Stu,

I tried to avoid "read all the lines in a loop and concatenate them in a variable"- I looked for something like readng the whole file (until EOF) into a buffer (< 1024 size) & use the buffer as a null-terminated string (that includes already real CRLFs).


You could call kernel32::ReadFile directly with the system plugin...