

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Read file line by line using C++
This is a C++ program to read file line by line.
Input
tpoint.txt is having initial content as “Tutorials point.”
Output
Tutorials point.
Algorithm
Begin Create an object newfile against the class fstream. Call open() method to open a file “tpoint.txt” to perform write operation using object newfile. If file is open then Input a string “Tutorials point" in the tpoint.txt file. Close the file object newfile using close() method. Call open() method to open a file “tpoint.txt” to perform read operation using object newfile. If file is open then Declare a string “tp”. Read all data of file object newfile using getline() method and put it into the string tp. Print the data of string tp. Close the file object newfile using close() method. End.
Example
#include <iostream> #include <fstream> #include <string> using namespace std; int main(){ fstream newfile; newfile.open("tpoint.txt",ios::out); // open a file to perform write operation using file object if(newfile.is_open()) //checking whether the file is open { newfile<<"Tutorials point \n"; //inserting text newfile.close(); //close the file object } newfile.open("tpoint.txt",ios::in); //open a file to perform read operation using file object if (newfile.is_open()){ //checking whether the file is open string tp; while(getline(newfile, tp)){ //read data from file object and put it into string. cout << tp << "\n"; //print the data of the string } newfile.close(); //close the file object. } }
Output
Tutorials point.
- Related Questions & Answers
- How to read complete text file line by line using Python?
- What are some of the fastest way to read a text file line by line using C#?
- Read last line from file in PHP
- How to read a file from command line using Python?
- How to read an entire line from a text file using Python?
- Print level order traversal line by line in C++ Programming.
- How to read only the first line of a file with Python?
- What's an easy way to read a random line from a file in the Unix command line?
- How to read only 5 last line of the text file in PHP?
- How to read a line from the console in C#?
- C++ program to read file word by word?
- C program to remove a line from the file
- How to compare two different files line by line in Python?
- How to write a single line in text file using Python?
- How to write into a file from command line using Python?
Advertisements