How to Read the First Line From a File in C

C++ Read File

Definition of C++ Read File

As we are well aware that C++ is one of the virtually widely used languages and it provides many features that allows the programmer to perform multiple tasks hands. In C++, working on file operations is every bit easy as working normally on the console operations using the cin and cout. Practically working with files is very commonly used in order to store the data permanently. fstream is the C++ library which is used to perform read and write file operations like iostream. The stream is nothing only used every bit the destination to inpt or output the data from 1 location to another. In that location are several datatypes of this library which perform specific tasks of file operations. 'ifstream' is one of the data types used specifically for reading files in C++.

How to Read File in C++?

As mentioned above, 'ifstream' data type of 'fstream' library is used to read the files of C++. But earlier reading, at that place are several tasks which are performed sequentially like opening the file, reading and closing it. Different data types are used for the specific purpose. Let's empathise the datatypes of 'ifstream' mentioned below:

Information Type Name Data Blazon Clarification
ofstream This data type is used to open the file or write something in the file. It represents the output file stream.
ifstream This information type is used to read the data from the file. It represents the input file stream.
fstream This data type represents a normal file stream and tin perform the tasks of both input and output file streams. Information technology tin can be used to open up, create, read and write in the file.

Given below is the step by step process to the file content in C++ :

1. Opening the Already Created File

In social club to read the information from the file, nosotros need to first open it. The opening of the file is done using ofstream or fstream object of the file. Files in C++ tin can be opened in different modes depending on the purpose of writing or reading. Hence, we need to specify the style of file opening along with the file name.

In that location are basically 3 default modes which are used while opening a file in C++:

  • ofstreamios: : out
  • fstreamios: : in | ios: : out
  • ofstreamios : :out

Syntax:

void open(filename, ios: : openmodemode_name);

two. Read the Information from The File

We can merely read the information from the file using the operator ( >> ) with the name of the file. We need to use the fstream or ifstream object in C++ in social club to read the file. Reading of the file line by line tin be done by simply using the while loop along with the function of ifstream 'getline()'.

3. Close the File

Equally we all know about the C++ memory management, every bit the program terminates, it frees all the allocated retentiveness and the used resource. Only still it is considered to be a skilful practice to close the file afterward the desired operation is performed.

Syntax:

void close();

Examples of C++ Read File

Below given are the few examples along with their outputs to demonstrate how the file read operation is performed in C++ :

Example #1

Lawmaking:
#include <iostream>
#include <fstream>
using namespace std;
intmain(){
char data[100];
// creating the file variable of the fstream data type for writing
fstreamwfile;
// opening the file in both read and write style
wfile.open up ("demo.txt", ios::out| ios::in );
// Asking the user to enter the content
cout<< "Please write the content in the file." <<endl;
// Taking the data using getline() function
cin.getline(data, 100);
// Writing the to a higher place content in the file named 'demp.txt'
wfile<< data <<endl;
// closing the file after writing
wfile.close();
//creating new file variable of data blazon 'ifstream' for reading
ifstreamrfile;
// opening the file for reading the content
rfile.open ("demo.txt", ios::in| ios::out );
// Reading the content from the file
rfile>> data;
cout<< data <<endl;
//closing the file after reading is done
rfile.shut();
return 0;
}

Output:

C++ Read File-1.1

Explanation: In the above example, nosotros have created two file variables of 'fstream' and 'ifstream' data blazon for writing and reading of the file respectively. In order to read or write the file, we demand to first open up the file using the open up() office and define its way. After opening, writing of the content in the file is done through the ( << ) operator and the file is airtight after writing using the shut() function. Now the file is opened again in order to read its content (using >> operator) and display it on the panel (using cout function). In club to release all the resources and gratis the allocated memory close() function is used.

Example #2

When the file user is reading is non establish.

Lawmaking:
#include<iostream>
#include<fstream>
using namespace std;
intmain()
{
char ch;
//creating the object of the data type 'istream'
ifstreamnew_file;
//opening the above file
new_file.open("demo1.txt",ios::in);
//checking whether the file is available or not
if(!new_file)
{
cout<<"Sorry the file you are looking for is not bachelor"<<endl;
return -ane;
}
// reading the whole file till the end
while (!new_file.eof())
{
new_file>>noskipws>>ch;
// press the content on the panel
cout<<ch;
}
//closing the file after reading
new_file.shut();
return 0;
}

Output:

C++ Read File-1.2

Explanation: In the above code, we are creating the object of the data blazon 'ifstream' in order to read the file named 'demo1.txt'. The file is opened using the open up() function in read mode using 'ios::in'. We take used the if and else statement to check whether the file is [resent or non. If the file is not institute, a proper message is displayed on the console. Otherwise, information technology volition read the whole file using the '>>' operator, and the content is printed on the console. In lodge to release all the resource and costless the memory, the shut() function is used.

Determination

The higher up clarification conspicuously explains how the reading of files is washed in C++ and the various information types of file stream used for specific operations. Equally working with files is very mutual in the practical world, it is very important to understand each and every file operation deeply before using them.

Recommended Articles

This is a guide to C++ Read File. Hither we also discuss the definition and how to read file in c++? along with different examples and its code implementation. You may besides take a wait at the following manufactures to acquire more than –

  1. C++ Lambda Expressions
  2. C++ shuffle()
  3. C++ endl
  4. C++ Aggregation

mackinitan1992.blogspot.com

Source: https://www.educba.com/c-plus-plus-read-file/

0 Response to "How to Read the First Line From a File in C"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel