In C programming a method is used to store the data on disk and retrieve the same data as when required from the disk without disturbing the data. To achieve this goal we use a concept called as file. isc-program-output.docx
In C programming we use three types of I/O functions(input/output functions).
1) Console I/O functions
2) Disk I/O functions
3) Port I/O functions
The disk I/O functions are used while manipulating the files and hence it is also called as file manipulation. While writing file manipulation programs we have to use data structure called as FILE which is defined in the library of standard I/O functions. Different file I/O functions are available. Functions like fopen(), fclose(), getc(), putc(), fprintf(), fscanf(), getw(), putw(), fseek(), ftell(), rewind() are normally used in file handling. Like file handling functions there are different file opening modes like r, w, a, r+, w+ and a+ mode are used.
below is the program which open the file test.txt and accept name and address of university and display the contents of the text file back on the screen.
Please include stdio.h, conio.h and stdlib.h header files before main function.
FILE * fp;
printf("\nEnter name and address of your university\n");
printf("\nPress ctrl + z key and enter key to stop inputting\n");
printf("\nOutput is as follows which shows name and address of your university\n");
printf("Error in opening the file\n");
A word file is attached herewith which include the output of the above program.
In above program firstly fp pointer of type FILE is declared. Using this FILE pointer we open the file test.txt in w+ mode first. Here w+ mode means if file with name test.txt is already exist then it destroy the previous contents of the file and open the file for writing. If file in not already exist then it creates the new file for writing.
When out inputting is over we have to press ctrl + z character so that compiler put end of character at the end of the file. This character is very important to recognize the end of file situation.
We use getchar() to accept character by character input from keyboard and using putc() function we write character by character data to file test.txt.
After writing is over we use fclose() function.
Now we reopen the test.txt file in reading mode. We read the file character by character using getc() and immediately print that character on monitor using printf().. When EOF(end of file) is encountered reading from file is over.
After reading the contents of file once again we use fclose().