[ Version ( since = "2.6" ) ]
[ CCode ( cname = "g_fopen" ) ]
public static FileStream? open (string path, string mode)

Opens a file indicated by a filename.

Mode is used to determine the file access mode.

Mode: Meaning: Explanation: File already exists: File does not exist:
"r" read Open a file for reading read from start failure to open
"w" write Create a file for writing destroy contents create new
"a" append Append to a file write to end create new
"r+" read extended Open a file for read/write read from start error
"w+" write extended Create a file for read/write destroy contents create new
"a+" append extended Open a file for read/write write to end create new

File access mode flag "b" can optionally be specified to open a file in binary mode. This flag has effect only on Windows systems.

On the append file access modes, data is written to the end of the file regardless of the current position of the file position indicator.

Example: Read chars from a stream:

public static int main (string[] args) {
// Opens "foo.txt" for reading ("r")
FileStream stream = ("filestream.vala", "r");
assert (stream != null);

// buffered:
char buf[100];
while (stream.gets (buf) != null) {
print ((string) buf);

return 0;

valac --pkg glib-2.0 GLib.FileStream.gets.vala



file name to associate the file stream to


character string determining file access mode


opened file stream on success, null on failure