C++ code on how to read characters from a file

This tutorial will show you a small example of C++ code on how to read a characters from a file, as well as to count the number lines that any particular file consist of. We will be creating the script and compiling the C++ on a Linux system. All distros will work the same, provided you have the G++ compiler installed, which we will cover as well.

In this tutorial you will learn:

  • How to install G++ on major Linux distros
  • C++ code for reading lines of a file
  • Compiling and running C++ program to read characters of file
C++ program that reads the number of lines in a file
C++ program that reads the number of lines in a file
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software G++ or other C++ compiler
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Install G++ on major Linux distros




In order to compile the C++ code that we present below, your system will have to have a C++ compiler installed. One of the best and most popular C++ compilers for Linux is G++. The GNU C++ Compiler is a compiler in Linux which was developed to compile C++ programs. The file extensions that can be compiled with G++ are .c and .cpp.

If you already have a different C++ compiler installed that you would prefer to use, that will work as well. Otherwise, use one of the following commands below to install the GNU C++ compiler with your Linux distribution’s package manager.

To install G++ on Ubuntu, Debian, and Linux Mint:

$ sudo apt install build-essential

To install G++ on CentOS, Fedora, AlmaLinux, and Red Hat:

$ sudo dnf install gcc-c++

To install G++ on Arch Linux and Manjaro:

$ sudo pacman -S gcc

Now that we have a compiler installed, move on to the section below to create the program.

C++ code to read characters from a file

Check out the C++ code below. The code will check for \n, the “new line character” and increase the number of lines stored in the number_of_lines integer variable. Every iteration will also print single character including \n to an output.

  1. First, create a file called my-input-file.txt which will contain some text. For example:
    welcome to
    linuxconfig.org
    c++
    
  2. Then, copy c++ code below to a file called read-characters.cpp:
    #include 
    #include 
    
    using namespace std;
    
    int main() {
    
    ifstream fin;
    fin.open("my-input-file.txt", ios::in);
    
    char my_character ;
    int number_of_lines = 0;
    
    	while (!fin.eof() ) {
    
    	fin.get(my_character);
    	cout << my_character;
    		if (my_character == '\n'){
    			++number_of_lines;
    		}
    	}
    cout << "NUMBER OF LINES: " << number_of_lines << endl;
    
    }
    



  3. Next, compile the code with the following command.
    $ g++ read-characters.cpp -o read-characters
    
  4. Execute the newly compiled binary file.
    $ ./read-characters
    

    NOTE: your text file must be located in the same directory as your executable read-characters program. The while loop and fin.get will add an extra new line character so you may want to use the following line to start counting at -1 instead:

    int number_of_lines = -1
    
C++ program that reads the number of lines in a file
C++ program that reads the number of lines in a file

The output of the program in this example will be:

welcome to
linuxconfig.org
c++

NUMBER OF LINES: 4

Closing Thoughts

In this tutorial, we saw a C++ code snippet that can be used to count the number of lines in a text file. We also learned how to install the GNU C++ compiler on a variety of Linux distributions, so that our code can be compiled and run in a variety of environments.



Comments and Discussions
Linux Forum