Writing and reading in a file


Reading Files in C Program

Before reading that file using the program, create a text file in the current directory and add some text to the file using a simple manual.
Follow the steps to read the file:

Create a file cursor.

To read any file, you must open a text file using the file cursor.
fpReadFile = fopen ("sampleFile.txt", "r");
Launch the loop until it reaches EOF (end of file). Now read each letter in the file using the getc ()
function.
while ((c = getc (fpReadFile))! = EOF)
putchar (c);
When the reading is complete, close the file.
fclose (fpReadFile);
Note: If you do not have a text file to read, fopen () will return NULL.

Here is a simple C file reading program.

#include <stdio.h> int main () { int c; FILE * fpReadFile; fpReadFile = fopen ("sampleFile.txt", "r"); print ("\ n"); if (fpReadFile) { printf ("\ nRead File ... \ n \ n"); while ((c = getc (fpReadFile))! = EOF) putchar (c); fclose (fpReadFile); } other { printf ("\ nError: Unable to open Read file."); } return 0; }

Writing Files in C Program.

File description step description:

As steps for reading a file, create a file cursor.
File * fpWriteFile;
Open File. Here you have to open the file by typing "w" mode.
fpWriteFile = fopen ("sampleFile.txt", "w");
Write the text in a file.
fprintf (fpWriteFile, "\ nEditing is a masterpiece.");
Close the file.
fclose (fpWriteFile);
Here is the program in C to write the file.

#include <stdio.h> int main () { FILE * fpSample; fpSample = fopen ("sampleFile.txt", "w"); if (fpSample) { fprintf (fpSample, "Art editing. \ nRead it. \ n"); fclose (fpSample); } other { printf ("\ nError: Unable to open compose file."); } }