Reading in a 2d Vector From a Txt File C++

In this article, we are going to evidence you how to read and write to a file in the C++ programming language by using several examples. To understand C++ file operations similar read and write, nosotros must first sympathize the concept of a stream in C++.

What is a Stream?

A stream is simply a flow of data or characters. There are 2 types of streams: input streams and output streams. An input stream is used to read the data from an external input device such as a keyboard, while an output stream is used to write information to the external output device such as a monitor. A file can be considered as both an input and output source.

In C++, we utilise a stream to send or to receive data to or from an external source.

We tin can use built-in classes to access an input/output stream, i.due east., "ios".

Here is the stream class hierarchy of the C++ programming language:

The "cin" and "cout" objects are used to read the data from the keyboard and to display the output on the monitor, respectively. In addition, "ifstream," which stands for "input file stream," is used to read a stream of information from a file, and "ofstream," which stands for "output file stream," is used to write a stream of data to a file.

The "iostram.h" file contains all the required standard input/output stream classes in the C++ programming linguistic communication.

Examples

Now that you understand the nuts of streams, we will discuss the following examples to help you to better sympathize file operations in C++:

  • Example 1: Open and Shut a File
  • Example 2: Write to a File
  • Example 3: Read from a File
  • Instance 4: Read and Write to a File
  • Example v: Read and Write to a Binary File

Instance 1: Open and Close a File

In this example program, we will demonstrate how to open/create a file and how to close the file in C++. As you tin see in the below program, we have included the library required for file operations.

To open and shut a file, we demand an object of ofstream. Then, to read or write to a file, nosotros have to open up the file. We have included the fstream header file at line number-ane so that we can admission ofstream class.

We have alleged a myFile_Handler as an object of ofstream inside the chief office. We can then apply the open() function to create an empty file and the close() role to close the file.

#include <fstream>

using namespace std;

int main( )
{
ofstream myFile_Handler;

// File Open
myFile_Handler.open ( "File_1.txt" ) ;

// File Close
myFile_Handler.shut ( ) ;
return 0 ;
}

Now, we will compile the programme and examine the output. Equally you can encounter in the output window below, the "File_1.txt" file was created after executing the program. The size of the file is zero since we accept non written whatsoever content in the file.

Example 2: Write to a File

In the previous example program, nosotros showed you how to open a file and how to close the file. Now, we volition show you how to write something in a file.

We can write to a file using the stream insertion operator, i.east., "<<". In this program, nosotros accept used the file handler and insertion operator to write two lines in the file. The insertion operator ("<<") indicates that we are inserting the cord into the output file stream object.

#include <fstream>

using namespace std;

int main( )
{
ofstream myFile_Handler;
// File Open up
myFile_Handler.open ( "File_1.txt" ) ;

// Write to the file
myFile_Handler << "This is a sample test File. " << endl;
myFile_Handler << "This is the second line of the file. " << endl;

// File Shut
myFile_Handler.close ( ) ;
return 0 ;
}

At present, nosotros will compile the above program and execute it. As you tin run into beneath, we accept successfully written to the file File_1.txt.

Instance iii: Read from a File

In the previous examples, we showed yous how to write content to a file. Now, let'due south read the content from the file that we created in Example-2 and display the content on the standard output device, i.east., the monitor.

We employ the getline() office to read the consummate line from the file and and so "cout" to print the line on the monitor.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int master( )
{
ifstream myFile_Handler;
string myLine;

// File Open in the Read Mode
myFile_Handler.open ( "File_1.txt" ) ;

if (myFile_Handler.is_open ( ) )
{
// Go on reading the file
while (getline(myFile_Handler, myLine) )
{
// print the line on the standard output
cout << myLine << endl;
}
// File Close
myFile_Handler.close ( ) ;
}
else
{
cout << "Unable to open the file!" ;
}
return 0 ;
}

Now, we will print the content of File_1.txt using the following control:  cat File_1.txt. Once we compile and execute the programme, it is clear that the output matches the content of the file. Therefore, we have successfully read the file and printed the content of the file to the monitor.

Example 4: Read and Write to a File

So far, we have showed you how to open, read, write, and shut a file. In C++, nosotros can also read and write to a file at the same time. To both read and write to a file, we have to become an fstream object and open the file in "ios::in" and "ios::out" way.

In this example, we starting time write some content to the file. And then, we read the data from the file and print it to the monitor.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main( )
{
fstream myFile_Handler;
string myLine;

// File Open
myFile_Handler.open ( "File_1.txt", ios:: in | ios:: out ) ;

// Cheque if the file has opened
if ( !myFile_Handler)
{
cout << "File did not open!" ;
exit ( 1 ) ;
}

// Write to the file
myFile_Handler << "1. This is another sample test File. " << endl;
myFile_Handler << "2. This is the 2nd line of the file. " << endl;

    myFile_Handler.seekg (ios:: beg ) ;

// Read the File
if (myFile_Handler.is_open ( ) )
{
// Keep reading the file
while ( getline(myFile_Handler, myLine) )
{
// print the line on the standard output
cout << myLine << endl;
}

// File Close
myFile_Handler.close ( ) ;
}
else
{
cout << "Unable to open the file!" ;
}
myFile_Handler.close ( ) ;
return 0 ;
}

Now, we volition compile and execute the program.

Instance 5: Read and Write to a Binary File

In this example, we are going to declare a class and so write the object to a binary file. To simplify this example, we accept declared the Employee class with a public variable emp_id. Then, nosotros will read the binary file and print the output to the monitor.

#include <iostream>
#include <fstream>

using namespace std;

class Employee
{
public :
int emp_id;
} ;

int main( )
{
ofstream binOutFile_Handler;
ifstream binInFile_Handler;

    Employee empObj_W, empObj_R;

// File Open
binOutFile_Handler.open up ( "Employee.dat", ios:: out | ios:: binary ) ;

// Check if the file has opened
if ( !binOutFile_Handler)
{
cout << "File did not open!" ;
go out ( ane ) ;
}

// Initialize empObj_W
empObj_W.emp_id = 1512 ;

// Write to the file
binOutFile_Handler.write ( ( char * ) &empObj_W, sizeof (Employee) ) ;
binOutFile_Handler.close ( ) ;

if ( !binOutFile_Handler.good ( ) )
{
cout << "Mistake occured during writing the binary file!" << endl;
go out ( 2 ) ;
}

// At present, permit's read the employee.dat file
binInFile_Handler.open ( "Employee.dat", ios:: in | ios:: binary ) ;
// Cheque if the file has opened
if ( !binInFile_Handler)
{
cout << "File did not open!" ;
exit ( iii ) ;
}

// Read the content of the binary file
binInFile_Handler.read ( ( char * ) &empObj_R, sizeof (Employee) ) ;
binInFile_Handler.close ( ) ;

if ( !binInFile_Handler.good ( ) )
{
cout << "Error occured during reading the binary file!" << endl;
go out ( 4 ) ;
}

// Print the output of empObj_R
cout << "Details of the Employee : " << endl;
cout << "Employee ID : " << empObj_R.emp_id << endl;

return 0 ;
}

Conclusion

Files are mainly used to shop the information, and they play an important role in real-world programming. In this article, we showed you how to employ various file operations with the C++ programming linguistic communication by working through several examples. Furthermore, we showed you how to read and write data into both text files and binary files.

Well-nigh the author

I am a passionate software engineer and blogger. I accept done my Masters in Software Technology from BITS PILANI University, India. I accept very skilful feel in real-time software development and testing using C, C++, and Python. Follow me at thecloudstrap.com.

nelsonmillue73.blogspot.com

Source: https://linuxhint.com/cplusplus_read_write/

0 Response to "Reading in a 2d Vector From a Txt File C++"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel