0c Create File
0c Create File
File Handling
In C, you can create, open, read, and write to files by declaring a pointer of
type FILE, and use the fopen() function:
FILE *fptr
fptr = fopen(filename, mode);
FILE is basically a data type, and we need to create a pointer variable to work
with it (fptr). For now, this line is not important. It's just something you need
when working with files.
To actually open a file, use the fopen() function, which takes two parameters:
Create a File
To create a file, you can use the w mode inside the fopen() function.
The w mode is used to write to a file. However, if the file does not exist, it
will create one for you:
Example
FILE *fptr;
// Create a file
fptr = fopen("filename.txt", "w");
#include <stdio.h>
int main() {
FILE *fptr;
return 0;
}
Tip: If you want to create the file in a specific folder, just provide an absolute
path:
This will close the file when we are done with it.