1

I need to read from file line by line and print it on the screen:

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

using namespace std;

int main()
{   
    ofstream out("note.txt");
    for (int i = 0; i < 10; i++)
        out << i << " " << (i<<1) << "\n";
    out.close();


    ifstream fin;
    fin.open("note.txt");

    string line;
    for (int i = 0; i < 10; ++i)
    {   
        getline(fin, line);
        cout << line << "\n";
    }
    return 0;
}

Is this approach correct? Cant I do it without a string variable (without string line in code)?

jonsno
  • 279
  • 4
  • 17
  • _"Cant I do it without a string variable (without string line in code)?"_ Could you explain further why you actually want to do so? – πάντα ῥεῖ Aug 29 '16 at 09:59
  • _is this approach correct?_ kind of questions are better suited for [codereview.stackexchange.com](http://codereview.stackexchange.com/). But, about your second question: where do you intend to read the variable to, if not into a variable? – Algirdas Preidžius Aug 29 '16 at 09:59
  • @AlgirdasPreidžius direct to screen. – jonsno Aug 29 '16 at 09:59
  • 4
    Here are some versions to print an ifstream directly to cout: http://stackoverflow.com/questions/675953/how-to-print-an-entire-istream-to-standard-out-and-string – Hayt Aug 29 '16 at 09:59
  • @Hayt: these answers don't provide a line by line solution (assuming that you may want to prepend line numbers). – stefaanv Aug 29 '16 at 10:03
  • Yeah it's not flexible in any way and seems more like a "homework" task. But from what is shown in the OP they do exactly what is tying to be done: Stream in input stream to an output stream. Without more context there are no general "good" solutions. – Hayt Aug 29 '16 at 10:06
  • `std::getline()` works with a reference to a string parameter to fill in the line. That makes it rather difficult to leave out the variable. You are probably looking for a version that returns a string. Even though that might seem handy in your case (`std::cout << std::getline(fin)`), most of the time you want checks whether the reading succeeded and a string-variable to process the input. – stefaanv Aug 29 '16 at 10:08
  • @stefaanv I tried cout << getline() also, it printed random alphanumeric string on screen. – jonsno Aug 29 '16 at 10:12
  • @sanjoe my answer is not what you are looking for? Would you mind to tell us what exactly do you need? – KostasRim Aug 29 '16 at 10:18
  • Possible duplicate of [Read file line by line](http://stackoverflow.com/questions/7868936/read-file-line-by-line) – kiner_shah Aug 29 '16 at 10:35
  • @samjoe: `std::getline()` returns std::istream which can be queried to see whether reading succeeded. standard use `while (std::getline(fin, line)) { ... }` – stefaanv Aug 29 '16 at 11:07

2 Answers2

4

Instead of using a for loop you can use a while loop:

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

using namespace std;

int main() {

    string line;
    ifstream out("note.txt");
    while(getline(out, line)) {
        cout << line << endl;
    }
    out.close();
}

If you are forced not to use strings then you can try a char buffer char buf[1024]. It must be pointed out that this approach is dangerous and error prone. If a line has more than 1024 characters then a buffer overflow will occur. Buffer overflow is the cause of many vulnerabilities and crashes. That being said, if you really have to use this method I would suggest you to be very careful by making the appropriate checks.

Eugene
  • 10,957
  • 20
  • 69
  • 97
KostasRim
  • 2,053
  • 1
  • 16
  • 32
  • close the file after use – rawjean Aug 29 '16 at 10:36
  • a fixed size buffer is a really bad idea Kostas. – Richard Hodges Aug 29 '16 at 10:55
  • @RichardHodges it is, but in the question he asks for a different approach without a `std::string`. I suspect that he meant a char buffer. That's why I included it in my answer. Thanks for pointing out though – KostasRim Aug 29 '16 at 11:00
  • OK, but if this post is to be educational I feel that the example using a fixed-size buffer ought to be fleshed out, complete with overflow protection. It will then be self-explanatory why we don't use fixed-size buffers unless an ancient API demands it. – Richard Hodges Aug 29 '16 at 11:06
  • @RichardHodges is it better now ? – KostasRim Aug 29 '16 at 11:16
  • 1
    :) certainly better. It's interesting that you didn't write the code to do it safely without a std::string. I don't blame you. It's difficult. – Richard Hodges Aug 29 '16 at 12:31
  • 1
    @RichardHodges not really, but reading a file using a buffer is more of c than c++. I have mentioned that a certain feature exist, from there, he can google the details. You can't baby spoon everybody :) – KostasRim Aug 29 '16 at 12:52
3

Copying a file verbatim is a simple as streaming out its stream buffer:

ifstream fin;
fin.open("note.txt");

std::cout << fin.rdbuf();
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142