Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a file line by line or a whole text file at once?

I'm in a tutorial which introduces files (how to read from file and write to file)

First of all, this is not a homework, this is just general help I'm seeking.

I know how to read one word at a time, but I don't know how to read one line at a time, or how to read the whole text file.

What if my file contains 1000 words? It is not practical to read entire file word after word.

My text file named "Read" contains the following:

I love to play games I love reading I have 2 books 

This is what I have accomplished so far:

#include <iostream> #include <fstream>  using namespace std; int main (){       ifstream inFile;   inFile.open("Read.txt");    inFile >> 

Is there any possible way to read the whole file at once, instead of reading each line or each word separately?

like image 951
Mody Avatar asked Oct 23 '12 17:10

Mody


People also ask

How can you read an entire file and get each line?

Method 1: Read a File Line by Line using readlines() readlines() is used to read all the lines at a single go and then return them as each line a string element in a list. This function can be used for small files, as it reads the whole file content to the memory, then split it into separate lines.

Which method can be used to read a whole line of text from a text file?

Python readline() is a file method that helps to read one complete line from the given file. It has a trailing newline (“\n”) at the end of the string returned. You can also make use of the size parameter to get a specific length of the line.

How do I read multiple lines in a text file?

The built-in readline() method return one line at a time. To read multiple lines, call readline() multiple times. The built-in readline() method return one line at a time. To read multiple lines, call readline() multiple times.


2 Answers

You can use std::getline :

#include <fstream> #include <string>  int main()  {      std::ifstream file("Read.txt");     std::string str;      while (std::getline(file, str))     {         // Process str     } } 

Also note that it's better you just construct the file stream with the file names in it's constructor rather than explicitly opening (same goes for closing, just let the destructor do the work).

Further documentation about std::string::getline() can be read at CPP Reference.

Probably the easiest way to read a whole text file is just to concatenate those retrieved lines.

std::ifstream file("Read.txt"); std::string str; std::string file_contents; while (std::getline(file, str)) {   file_contents += str;   file_contents.push_back('\n'); }   
like image 63
111111 Avatar answered Sep 22 '22 13:09

111111


I know this is a really really old thread but I'd like to also point out another way which is actually really simple... This is some sample code:

#include <iostream> #include <fstream> #include <string> using namespace std;  int main() {      ifstream file("filename.txt");     string content;      while(file >> content) {         cout << content << ' ';     }     return 0; } 
like image 33
user2673553 Avatar answered Sep 19 '22 13:09

user2673553