Fopen

From SA-MP Wiki

(Redirected from Function:fopen)
Jump to: navigation, search

fopen

Image:Farm-Fresh text lowercase.png Note: This function name starts with a lowercase letter.


Description:

Open a file (to read from or write to).


Parameters:
(const name[], filemode:mode = io_readwrite)
name[]The path to the file to open (if just a filename is specified, it will open the file with the name specified in the 'scriptfiles' folder).
modeThe mode to open the file with (default: io_readwrite).


Return Values:

Returns the file handle. This handle is used for reading and writing. 0 if failed to open file.


Image:32px-Circle-style-warning.png

Important
Note

This function can't access files outside the 'scriptfiles' folder!


Image:32px-Circle-style-warning.png

Warning

If you use io_read and the file doesn't exist, it will return a NULL reference. Using invalid references on file functions will crash your server!


Modes

io_read      Reads from the file.
io_write     Write in the file, or create the file if it does not exist. Erases all existing contents.
io_readwrite Reads the file or creates it if it doesn't already exist.
io_append    Appends (adds) to file, write-only. If the file does not exist, it is created.

Example Usage:

// Open "file.txt" in "read only" mode
new File:handle = fopen("file.txt", io_read),
 
	// Initialize "buf"
	buf[128];
 
// Check, if the file is opened
if(handle)
{
	// Success
 
	// Read the whole file
	while(fread(handle, buf)) print(buf);
 
	// Close the file
	fclose(handle);
}
else
{
	// Error
	print("The file \"file.txt\" does not exists, or can't be opened.");
}
// Open "file.txt" in "write only" mode
new File:handle = fopen("file.txt", io_write);
 
// Check, if file is open
if(handle)
{
	// Success
 
	// Write "I just wrote here!" into this file
	fwrite(handle, "I just wrote here!");
 
	// Close the file
	fclose(handle);
}
else
{
	// Error
	print("Failed to open file \"file.txt\".");
}
// Open "file.txt" in "read and write" mode
new File:handle = fopen("file.txt"),
 
	// Initialize "buf"
	buf[128];
 
// Check, if file is open
if(handle)
{
	// Success
 
	// Read the whole file
	while(fread(handle, buf)) print(buf);
 
	// Set the file pointer to the first byte
	fseek(handle, _, seek_begin);
 
	// Write "I just wrote here!" into this file
	fwrite(handle, "I just wrote here!");
 
	// Close the file
	fclose(handle);
}
else
{
	// Error
	print("The file \"file.txt\" does not exists, or can't be opened.");
}
// Open "file.txt" in "append only" mode
new File:handle = fopen("file.txt", io_append);
 
// Check, if file is open
if(handle)
{
	// Success
 
	// Append "This is a text.\r\n"
	fwrite(handle, "This is a test.\r\n");
 
	// Close the file
	fclose(handle);
}
else
{
	// Error
	print("Failed to open file \"file.txt\".");
}

Related Functions

The following functions may be useful, as they are related to this function in one way or another.


  • fopen: Open a file.
  • ftemp: Create a temporary file stream.
  • fgetchar: Get a character from a file.
  • fseek: Jump to a specific character in a file.
  • fexist: Check, if a file exists.
  • fmatch: Check, if patterns with a file name matches.
Personal tools
Navigation
Toolbox
In other languages