Reading and Writing in files: Difference between revisions
From NSIS Wiki
Jump to navigationJump to search
m (Wikipedia python library) |
m (Reverted edits by 115.248.79.66 to last version by Kichik) |
||
(11 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
{{PageAuthor|n0On3}} | |||
== Description == | == Description == | ||
This example is to show you how easy is to read/write in files. | This example is to show you how easy it is to read/write in files. | ||
== | |||
== Write (overwrite) == | |||
This will overwrite SomeFile.txt contents or create it and put "hello" inside: | This will overwrite SomeFile.txt contents or create it and put "hello" inside: | ||
<highlight-nsis> | <highlight-nsis> | ||
Line 8: | Line 11: | ||
FileClose $4 | FileClose $4 | ||
</highlight-nsis> | </highlight-nsis> | ||
== | == Write (append) == | ||
If the file exists and you don't want to lose its contents | If the file exists and you don't want to lose its contents use this: | ||
<highlight-nsis> | <highlight-nsis> | ||
FileOpen $4 "$DESKTOP\SomeFile.txt" a | FileOpen $4 "$DESKTOP\SomeFile.txt" a | ||
Line 19: | Line 22: | ||
</highlight-nsis> | </highlight-nsis> | ||
That "$\r$\n" means "carriage return + new line". This is how windows knows that there's a new line in the file. | That "$\r$\n" means "carriage return + new line". This is how windows knows that there's a new line in the file. | ||
== | == Read == | ||
For reading files:
<highlight-nsis>
FileOpen $4 "$DESKTOP\SomeFile.txt" r | For reading files: | ||
<highlight-nsis> | |||
FileOpen $4 "$DESKTOP\SomeFile.txt" r | |||
FileSeek $4 1000 ; we want to start reading at the 1000th byte | FileSeek $4 1000 ; we want to start reading at the 1000th byte | ||
FileRead $4 $1 ; we read until the end of line (including carriage return and new line) and save it to $1 | FileRead $4 $1 ; we read until the end of line (including carriage return and new line) and save it to $1 | ||
Line 26: | Line 31: | ||
FileClose $4 ; and close the file | FileClose $4 ; and close the file | ||
</highlight-nsis> | </highlight-nsis> | ||
The variables we use to read/write can only hold up to 1024 bytes, but this is | The variables we use to read/write can only hold up to 1024 bytes, but this is hardly a problem. | ||
Notice that every example used a different mode to open the file "w" "a" and "r". | Notice that every example used a different mode to open the file "w" "a" and "r". | ||
[[Category:Code Examples]] |
Latest revision as of 20:14, 4 November 2012
Author: n0On3 (talk, contrib) |
Description
This example is to show you how easy it is to read/write in files.
Write (overwrite)
This will overwrite SomeFile.txt contents or create it and put "hello" inside:
FileOpen $4 "$DESKTOP\SomeFile.txt" w FileWrite $4 "hello" FileClose $4
Write (append)
If the file exists and you don't want to lose its contents use this:
FileOpen $4 "$DESKTOP\SomeFile.txt" a FileSeek $4 0 END FileWrite $4 "$\r$\n" ; we write a new line FileWrite $4 "hello" FileWrite $4 "$\r$\n" ; we write an extra line FileClose $4 ; and close the file
That "$\r$\n" means "carriage return + new line". This is how windows knows that there's a new line in the file.
Read
For reading files:
FileOpen $4 "$DESKTOP\SomeFile.txt" r FileSeek $4 1000 ; we want to start reading at the 1000th byte FileRead $4 $1 ; we read until the end of line (including carriage return and new line) and save it to $1 FileRead $4 $2 10 ; read 10 characters from the next line FileClose $4 ; and close the file
The variables we use to read/write can only hold up to 1024 bytes, but this is hardly a problem. Notice that every example used a different mode to open the file "w" "a" and "r".