Creating (or) Opening a file
To create a new file or open an existing file, we need to create a file pointer of FILE type. Following is the sample code for creating file pointer.
File *f_ptr ;
We use the predefined method fopen() to create new file or to open an existing file.
There are different modes in which a file can be opened.
Consider the following code...
File *f_ptr ;
*f_ptr = fopen("abc.txt", "w") ;
The above example code creates a new file called abc.txt if it does not exists otherwise it is opened in writing mode.
fopen is a standard function which is used to open a file.
- If the file is not present on the system, then it is created and then opened.
- If a file is already present on the system, then it is directly opened using this function.
fp is a file pointer which points to the type file.
In C programming language, there different modes are available to open a file and they are shown in the following table.
Example
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen ("D://data.txt", "w");
}
No comments:
Post a Comment