File Reading (file_contents.nvgt)
File Contents Library
Facilitates the easy reading and writing of strings in files.
Functions
get_file_contents
Reads the contents of a file as a string.
string get_file_contents(string filename)
Arguments:
- string filename: the name of the file to read.
Returns:
The contents of the file on success, an empty string on failure.
Example:
#include "file_contents.nvgt"
void main() {
string filename = input_box("Filename", "Enter the name of a file to read.", "");
string contents = get_file_contents(filename);
if (contents == "")
alert("Example", "Either the file was not found, or it contained no text.");
else {
clipboard_set_text(contents);
alert("Example", "The contents of the file is now on your clipboard.");
}
}
put_file_contents
Writes a string to a file.
bool put_file_contents(string filename, string content, bool append = false)
Arguments:
string filename: the name of the file to write to.
string content: the content to write.
bool append = false: specifies whether the current contents of the file should be overwritten when writing.
Returns:
true on success, false on failure.
Example:
#include "file_contents.nvgt"
void main() {
if (!put_file_contents("example.txt", "This is an example"))
alert("Example", "Failed to write the file.");
else
alert("Example", "Successfully wrote the example file.");
}