C++

Using Fin.get in C++

Fin is the name of a stream variable and it is used in file stream management. The purpose of both fin and fout is the same as cin and cout in iostream. These two streams are defined in the fstream library. Fin is used to open the file and get the characters from the file and display them.

Implementation of fin.get()

Example 1
This example deals with the total number of characters that are present inside the file. For this purpose, a counter variable will be initialized to the program.

First, we will talk about the libraries that support the source codes to be executed. The iostream is used to embed the functionalities of display like cout and input features like cin, so we need to use this in our header file.

Second, as we are concerned with the file system like to open and close files, and in applying other functions of filing, we need a library that has all the file streaming functionalities.

#include <iostream>
#include <fstream>

Let us start the source code logic inside the main program. In file streaming, ifstream is used to open the file and read the data in it, where the fin is the object.

Ifstream fin("fruit.txt");

The name of a sample file having two names is ‘fruit.txt’.

The variable counter that counts the total characters in the file is initialized as zero at the start. As the counter counts the characters, the data type of counter is also declared as the character. The built-in function fin.get is initialized here that reads the characters from the file.

A while loop is used that will iterate ‘till the end of the file comes. Inside the parameter of while loop, fin.eof() or ‘end of file’ function is declared. This function means to get the value ‘till the end of the file.

While ( !fin.eof())

Inside the while loop, the counter variable is incremented first.

C = fin.get ();

After that, the character at the first position is displayed, and then the fin.get () method is called to get the next character. Inside the loop again this continues, the character is obtained through the get () method and the counter is incremented and then both the values are obtained. When the last character is counted, the loop terminates and outside the loop body, the counter value as a whole is displayed.

Save the file with the .c extension. To execute the code, we will use the Ubuntu terminal. In Linux, for C++ code, a compiler is needed to compile the source code: that compiler is g++. Input and output file are needed to display the results that are stored by –O.

$ g++ -o file file.c
$ ./file

Upon execution, you will see the results below. Each line contains a number that is the count of the character of the word. The space between two words and the next line is also counted as characters.

The total number of characters is displayed at the end.

Example 2

This example deals with the function of fsteams of the file system. From our PC, we will load the file, open it, update it by writing in it, and display its content after updating it by using different streams of a file applicable according to condition.

Suppose we have a simple text, fruit file, that contains names of fruits. Refer below.

We will use this file in our source code. After loading both the libraries as we have defined in the first program, inside the main program, a character variable and a character array is defined that will be used later on.

From fstream, ofstream is used, that is the output file stream, and used to write contents in the file. This feature contains an object through which the file is loaded and opened in the program.

Ofstream fout("sruits.txt");

An if-statement is used here that checks the object. If the file opens, then it is working. But if it is unable to open the file, simply display an error message that the file can’t be opened. Outside the ‘if’ body, from fout, all the contents that we want to write in the file are displayed here. These three lines are entered into the file. The previous ones are replaced with them, in case the file already has content. Otherwise, an empty file will be created to write these lines. After adding a content, the file is closed.

Let’s check the file through the ‘if’ statement. It checks whether the content is written to the file or not. An error will be displayed.  In the second case, proceed forward. After that, use ifstream to open the file.

Ifstream fin("fruits.txt")

Again, check to see if the file can be opened or not.

The next step is to fetch the record that we have entered by applying some conditions. These conditions are up to the user at all. We have applied logic to get the first three characters of the first string in the file. Here we have taken help from the for loop that iterates up to the 3rd character of the first word in the sentence inside the loop.

Fin.get(ch);

It will get the character and these characters will be displayed through cout whereas the rest of the words in the first statement of the file are displayed again by the fin.get () function. But here, the array of characters is used in the parameters. We used the object of a character array and the size that will store the characters of strings as it is.

Fin.get (str, 255);

The sentences as a whole are the strings of characters. We will use a built-in function getline ()  to get all the two sentences after the first one without applying any condition.

Fin.getline(str, 255)

After we displayed all the data we wanted to, the file needs to be closed. This will be done through:

Fin.close ()

The ‘if’ statement plays the role of checking by using a fin.good() function. If it is true, it means that there is no error. If it is false, an error message is displayed.

Now, let us execute the above code in the terminal.

You will see that through the get() function, the first three characters and the remaining characters are displayed. Whereas, through the getline() function, both the sentences are displayed. Throughout the execution, we got no error message.

Conclusion

The fin.get() function can be used in different ways, but the purpose of this function remains the same: to fetch the record of the file. We implemented two different examples by using this function in opening and adding data to the file, and then by using other function streams of filing. We displayed the data by applying the conditions and closed the file. By following this approach, fin.get can easily be used in the file system.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.