C++

Read Text File Into 2d Array C++

In C++, a 2d array is the utmost straightforward category of a multi-dimensional array. It’s represented as an array of arrays. A two-dimensional array is located 1 row after another in the system memory. The shared memory of the whole two-dimensional array is determined by the location of the first bit of storage.

In C++, we will employ the ‘ifstream’ method to read the relevant text file into a 2d array. This will assist us in deploying the extraction operator to regain certain data. This article will examine different approaches to reading a text file into a two-dimensional array.

Use fstream() Method

This step will employ the fstream() function to read a file into a two-dimensional array. The code of this instance is affixed to the subsequent image.

At the beginning of the program, we have to include some libraries that deal with filing. These libraries are <fstream> that are responsible for opening files for writing. We can also integrate the <ofstream> library for this purpose. The strings are exchanged between the code’s comment stream and the interface of the system by using the header file <sstream>. To handle the functionalities of string, we apply <string> library. The last one is <vector>.

After including all the required libraries, we start the body of the main() function. We provide the pointer as its arguments with the data type’s integer and character. In the main function, we have utilized ‘std,’ which stands for ‘standard.’ It is included for executing the header input-output stream in the code. We created a file having the name ‘example.txt.’ Using the function fin(), we open the file by entering the file name as the function’s parameters.

Next, we employ std::vector. Then we apply std::string line to read line or string. We will utilize a ‘while’ loop where the condition is applied on fin() that is responsible for acquiring the incoming information. The getline() function is also utilized to get all file lines. Within the ‘while’ loop, we get words by using the ‘std::cout’ statement. The words of the files are printed, and after each word, the cursor will move to the next line.

Wherever the code gets the actual next line of the file on the screen while showing output, the word ‘newline’ is also printed on the screen. To end the program, we enter the ‘return 0’ command. The output of this instance is affixed to the subsequent image.

Use ‘while’ Loop

We can read the text file into a two-dimensional array in C++ by using the ‘while’ loop in the program. The code of this instance is affixed to the subsequent image.

First of all, we introduce some header files used to manage the file system, strings, and so on. Then we add a standard namespace, calling the main() function. We declare a variable with string data type having a 2D array with 6×3 dimensions. Next, we utilized the ifstream() function and entered the name of the desired file as a parameter.

Make sure the text file and code file are in the same directory; otherwise, we have to give the complete path of the text file. If the file name or path is incorrect, we get the message using the ‘cout’ statement. If the file name and path are correct, we initialize two variables, ’x’ and ‘y’, for the dimensions of the 2D array and start the ‘while’ loop, where we check the words of the required file and label their positions in the array. Then we do increment in their positions.

Now we print the file and their position in the array, so we have to use two ‘for’ loops one is for the x-axis, and the second is for the y-axis. We will represent the array of contents. The output of this instance is affixed to the subsequent image.

Use ifstream() Method

With the help of the ifstream() method, we simply read the text file into a two-dimensional array. The code of this instance is affixed to the subsequent image.

In this example, we integrate three libraries. The first one is <iostream>, which is responsible for input and output flow. The second is <fstream>, which is used for file systems and managing file streaming. The last one is <string>, which handles all strings in the code and the files that we want to read through this code. After this, the standard namespace is utilized. We initialized the array and set the size of the array. We take two short loops.

Similarly, we declare a string-type variable named ‘line’ that manages the lines of files. Here we utilize the myfile() function to get the file name from the user and save it in this file. We apply the ‘If’ statement to check whether the defined file is open or not. If not, then display the message ‘unable to open file.’ This is caused by entering the wrong file, or the path of the file being wrong. When the code fulfills the condition of the ‘if’ statement, the code will read the file line by line in the ‘while’ loop, it continuously reads the content of the file until the condition is false.

When the specified condition is false, the reading is closed. So for this cause, we enter the system(“PAUSE”). The output of this instance is affixed to the subsequent image.

Conclusion

In C++ language, we can write the data, read the data and modify the data in the files. In this article, we have elaborated on three methodologies for how we read a text file into a 2d array. These strategies are adequately explained by executing related programs of these techniques. We have to integrate the header file <ifstream> in our programs to acquire the text files into a 2d array.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content