In the world of computer science, file handling is a very important concept. A file is a type of portable storage that is used to store any type of data. Before a programmer applies any algorithm on a particular file, they need to open that file using the fopen()
command.
fopen()
only has 2 parameters passed into it:
FILE *filePointer;
filePointer = fopen("filename.txt", "w");
The following code opens a file and writes “Educative!” in it.
#include <cstdio>#include <cstring>using namespace std;int main(){int c;FILE *filePointer;filePointer = fopen("filename.txt", "w");char sampleString[20] = "Educative!";if (filePointer){for(int i=0; i<strlen(sampleString); i++)putc(sampleString[i],filePointer);}fclose(filePointer);}