fopen()
Opens a file.
Synopsis
file fopen( string filename, string mode );
Parameters
Parameter | Description |
---|---|
filename | File to be opened. |
mode | mode |
Return value
The function fopen() returns a file pointer or returns 0 if unsuccessful.
Error
Missing or wrong arguments.
Description
The function fopen() opens the file with the specified name. The file can be accessed with the return value using further file functions. The following modes exist:
mode | Meaning |
---|---|
r | Open existing file for reading |
w | Create file for writing. If a file with the given name already exists, its contents will be lost. |
a | Create file for writing. If a file with the given name already exists, the new text will be appended at the end. |
r+ | Open file for reading and writing. |
rb | Binary Read |
w+ | Create file for writing and reading. If a file with the given name already exists, its contents will be lost. |
wb | Write a binary file. |
wb+ | Create a binary file for writing and reading. |
a+ | Create file for writing and reading. If a file with the given name already exists, the new text will be appended at the end. |
-
Please consider that the encoding of the stated filename must match the current codepage of the operating system.
-
To prevent possible change to the line ending encoding the parameter "w" should be used instead of "wb".
Example
The following example opens a binary file for writing and editing. Furthermore, the file is opened in the file editor.
main()
{
file f;
// our file
int err;
// error code
string fileName = tmpnam();
f = fopen(fileName, "wb+");
//Open a binary file for writing and reading
err = ferror(f);
//Output possible errors
if ( err )
{
DebugN("Error no. ",err," occurred");
return;
}
string content = "This is my file \n";
fputs(content, f);
//Write to the file
fclose(f);
//Close file
fileEditor(fileName, FALSE);
//Open the file in the file editor
remove(fileName);
}
Assignment
File function
Availability
CTRL