open


Description:

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

A wrapper for the stdio `fopen()` function.

The `fopen()` function opens a file and associates a new stream with it.

Because file descriptors are specific to the C library on Windows, and a file descriptor is part of the `FILE` struct, the `FILE*` returned by this function makes sense only to functions in the same C library. Thus if the GLib-using code uses a different C library than GLib does, the FILE* returned by this function cannot be passed to C library functions like `fprintf()` or `fread()`.

See your C library manual for more details about `fopen()`.

As `close()` and `fclose()` are part of the C library, this implies that it is currently impossible to close a file if the application C library and the C library used by GLib are different. Convenience functions like set_contents_full avoid this problem.

Example: Read chars from a stream:

public static int main (string[] args) {
// Opens "foo.txt" for reading ("r")
FileStream stream = FileStream.open ("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

Parameters:

mode

a string describing the mode in which the file should be opened

filename

a pathname in the GLib file name encoding (UTF-8 on Windows)

Returns:

A `FILE*` if the file was successfully opened, or null if an error occurred