Header Ads

File operation:C Programming Language - Part 9

File operation



We may have to write programs where the program will save some data on the computer. The easiest way to save data on a computer is to put it in the file. In this file we will know how to put data in a file and then again working with the data from that file again.



File declare:

To work with a file is to declare it. It is declared by FILE pointers. Such as

FILE * MyFile;

FILE is written in uppercase and MyFile is the pointer variables. It basically creates a buffer between the computer memory and the file. After creating the pointer variable, we can open the file. For him, use the fopen function. Which are usually written as:

MyFIle = fopen (file-name, file-type);


File name and type two strings. File type is going to open the file in any mood. Such as Read Only, Write Only or both. Such as

MyFile = fopen ("myfile.txt", "w");
File-type can be any one of the following table


Type jobs
R Just open the file to read.
W
Open the file to write something right.
If there is anything in the file, it will be deleted.
If not, the file will be automatically generated.
A
Opening the file to write something right.
If there is anything in the file, then it will be right on the end.
If not, the file will be automatically generated.
R + Open the file to read or write. And some text at the beginning of the file.
W + open the file to read or write.
A + open to read or write the file. And writing something at the end of the file.

Write data to the file:

The fputs function is used to open some files by typing them. This is how to write:


Fputs ("Writing to a file using fopen.", MyFile);


If the file is read or written, the file is closed or closed, the fclose function is used for it.


Fclose (MyFile);


A complete program

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
Int main ()
{
    FILE * MyFile;
    Char string [10];
    MyFile = fopen ("myfile.txt", "w");
    Fputs ("Writing to a file using fopen.", MyFile);
    Fclose (MyFile);
    Return 0;
}
Open the C project that has been saved in the folder. You can see a file named myfile.txt inside it. If you open the file and see it, it will be written in the Writing to a file using fopen.


The above program can be modified. Try writing anything that you like. You will see that before you write it, every time you write the program, the previous text is deleted. Do not delete the previous text, so that the new text becomes associated with the previous text, for that the program can write as:

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
Int main ()
{
    FILE * MyFile;
    Char string [10];
    MyFile = fopen ("myfile.txt", "a");
    Fputs ("Writing to a file using fopen. \ N", MyFile);
    Fclose (MyFile);
    Return 0;
}
Here's just changing the file title. Now every time you run the program, a text will be added to the myfile.txt file.
In the above example, the file will be saved in the folder that is in our program. You can tell any other way you want. See the following program:

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
Int main ()
{
 FILE * MyFile;
 MyFile = fopen ("F: \ myfile.txt", "a");
 If (MyFile! = NULL)
 {
     Fputs ("Writing to a file using 'fopen' example.", MyFile);
     Fclose (MyFile);
 }
 Return 0;
}
If you have an F drive on your computer, the file myfile.txt will be saved to the F Drive. You can say any other drive if you want. You can also say folders. If your folder is F: \ My Folder then you have to type it as: F: \\ My Folder \\ myfile.txt. Due to the use of backslashes (\) in C programming, double backslashes have to be used.




Read data from file

Fscanf is used to read data from files.

Please run the program below. Before running the program, be aware that the folder in which your program is in the folder contains a file named myfile.txt. And there is some text in that file. Or the program will look wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
Int main ()
{
    FILE * MyFile;
    Char string [1000];
    MyFile = fopen ("myfile.txt", "r +");
    While (! Feof (MyFile)
    {
        Fscanf (MyFile, "% s", string);
        Printf ("% s", & string);
    }
    Fclose (MyFile);
    Return 0;
}
Runs the program in myfile.txt file and runs the console. Let's know how the program worked.


The feof with end-of-file is checked. That is, the file data will be read by fscanf as long as there is no data in the file.
Fscanf has three parameters. Fscanf (file-name, data-type, variable);
File-name is the name of the file. The data from that file will be read. Data-type is the file type of the file. Or any type of data will be read. Here char has read data. If you wish, you can read the int or floating point data. However, it should be ensured that the files have integer or floating point data.
Variable is the name of the variable where the data will be saved from the file.
We still have our C program in the folder that is open or read the file. If we wish we can open the file from any directory. File-name will be replaced with full file path. See the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>

File operation

Int main ()
{
    FILE * MyFile;
    Char string [1000];
    MyFile = fopen ("F: \\ myfile.txt", "r +");
    While (! Feof (MyFile)
    {
        Fscanf (MyFile, "% s", string);
        Printf ("% s", & string);
    }
    Fclose (MyFile);
    Return 0;
}
It was a basic concept about file operation. Now you can work with files you like. Keep any data type data in the file. Then try to read it. You can use the loop. You can use the string.


Our YouTube Channel:Clik Here



No comments

Theme images by sebastian-julian. Powered by Blogger.