Home » C++ » How to read line by line or a whole text file at once?

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

Posted by: admin November 30, 2017 Leave a comment

Questions:

I’m in a tutorial which introduces files (how to read and write from\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 each 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 separate?

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');
}  

Questions:
Answers:

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;
}

Questions:
Answers:

I think you could use istream .read() function. You can just loop with reasonable chunk size and read directly to memory buffer, then append it to some sort of arbitrary memory container (such as std::vector). I could write an example, but I doubt you want a complete solution; please let me know if you shall need any additional information.

Questions:
Answers:

Well, to do this one can also use the freopen function provided in C++ – http://www.cplusplus.com/reference/cstdio/freopen/ and read the file line by line as follows -:

#include<cstdio>
#include<iostream>

using namespace std;

int main(){
   freopen("path to file", "rb", stdin);
   string line;
   while(getline(cin, line))
       cout << line << endl;
   return 0;
}

Questions:
Answers:

Another method that has not been mentioned yet is std::vector.

std::vector<std::string> line;

while(file >> mystr)
{
   line.push_back(mystr);
}

Then you can simply iterate over the vector and modify/extract what you need/