C Programming -Lecture 10 :File Input Output
As soon as the computer user knows about the file. This program is made of a variety of files. We made our program to duplicate files and then they can use or modify.
We basically use a three-step file kari
1. Open the file
II. Some of the files are read from or written to the file
3. File Close
Our need to use a file with a file handle. We have identified a phairake through it. It is a pointer to type FILE what it is like to declare
FILE * fp;
We have opened a file with fopen returns.
fopen
fp = fopen (filename, mode);
The use of this function is to open a file. This function has two parameters. After opening the first name of the file and the second file will be how to use it first.
r - read mode, the file will be opened. Thus, if you do not use the FILE pointer to file will not return.
W - read mode, the file will be opened. If you do not file a new file will be created. All data will be deleted from the file when the file and the file will be written from scratch.
A - append to the end of the file before it is used in this mode is to start writing. If you do not file a new file will be created.
+ Read and write can be used simultaneously. Then you can decide how to use it or not.
r + - if you need to read and write at the same time to start reading from the beginning, and the file is used in this mode.
w + - at the same time if you need to read and write, and to start writing from the beginning of the file is used in this mode. If you do not file a new file will be created.
A + - and if you need to read and write at the same time to start writing at the end of the file is used in this mode. If you do not file a new file will be created.
Below are a few examples.
fp = fopen ( "myfile.txt", "w");
If you will create a file named myfile.txt in which we will be able to write.
read mode to open the file will be written as
fp = fopen ( "names.txt", "r");
The file must be names.txt. So we are now able to read from the file.
fprintf
But it's like the printf function is used to write to the file.
As we write this
printf ( "Hello, World!");
If the console Hello, World! Will be written.
fprintf with the tai file with the handle should be at.
fprintf (fp, "Hello, World!");
If the file Hello, World! Will be written.
fscanf
But it's like scanf This function is used to read from the file.
As we write this
NUM int;
scanf ( "% D", & NUM);
If the input from the keyboard with num variable will hold a purnasankhya.
tai fscanf the case with the file handle should be at.
NUM int;
fscanf (FP, "% D", & NUM);
If you file a purnasankhya of the num variable would be.
fclose
File at the end of this function can be used to close the file.
Thus, if the file will be closed
fclose (fp);
Therefore see a full example
#include "stdio.h"
int main ()
{
FILE * fp;
fp = fopen ( "myfile.txt", "w");
return 0;
}
When you run it, it will create a file Hello, World! Will be written. The file is written correctly, you can see whether the open.
Please note that the names of some of the function of the file can be read from or written to the file.
Fputc
fgetc
fread
fwrite
ftell
fseek
fflush.


No comments: