Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ using ifstream to read file

Tags:

c++

I have some questions regarding using std::ifstream in C++.

Most are general questions I couldn't find answers to, so might be useful for others, too.

Anyways, I use #include <fstream> and made a variable char line[20].

There is a text file that has several lines of the pattern jxxx (where the xs are numbers), like this:

j1234
j5678
j1111
(and so on)

So, I do this:

#include <iostream>
#include <fstream>

char line[20]
ifstream rfile;
rfile.open("path-to-txt-file");
while (!rfile.eof()) {
    rfile.getline(line, 20); (why is 20 needed here?)
    cout >> data >> endl;
    }
rfile.close();

So the concerns are:

  1. why is the number needed in the getline method?

  2. does line have \0 at the end automatically? because when we create char vars like char text[5]; text = "abc1", it is actually saved as "acd1\0" and what happens to the endlines in the file (\n) after each of the jxxx lines? (I would like to use the lines in more complex code than this, so want to know)

  3. Will the program move to the next line automatically? If not, how do I tell it to go to the next line?

like image 473
Atem Avatar asked Oct 12 '17 21:10

Atem


People also ask

Does ifstream open a file?

std::ifstream::open. Opens the file identified by argument filename , associating it with the stream object, so that input/output operations are performed on its content. Argument mode specifies the opening mode. If the stream is already associated with a file (i.e., it is already open), calling this function fails.

Does ifstream read or write?

ifstream : Stream class to read from files. fstream : Stream class to both read and write from/to files.

Can you use getline to read from a file?

Use std::getline() Function to Read a File Line by Line The getline() function is the preferred way of reading a file line by line in C++. The function reads characters from the input stream until the delimiter char is encountered and then stores them in a string.


1 Answers

  1. you are calling std::ifstream::getline(), which takes a char* pointer to a buffer for output. getline() requires you to specify the max size of that buffer so it won't overflow. If you want to handle variable-length lines without worrying about overflows, you should change line to std::string and use std::getline() instead.

  2. if successful, std::ifstream::getline() will null-terminate the output buffer. Which means at most, getline() will read 1 less than the max size requested, so make sure you include room for the null terminator in the size you pass in (getline() may read fewer, if it encounters a line break or EOF). As for the line breaks themselves, they are swallowed by getline(), they will not appear in the buffer.

  3. yes, the code will move to the next line automatically, so you can just keep calling getline() in a loop.

On a side note:

  • while (!rfile.eof()) is bad to use. Use while (rfile) instead. Or better, while (rfile.getline(line, 20)). Either way will account for any errors that occur in both open() and getline(), but the latter ensures that cout is not called if getline() fails.

  • cout >> data >> endl; is also wrong. You need to use << with std::cout, and data should be line instead.

Try this instead:

#include <iostream>
#include <fstream>

char line[20];
std::ifstream rfile;
rfile.open("path-to-txt-file");
if (rfile.is_open()) {
    while (rfile.getline(line, 20)) {
        std::cout << line << std::endl;
    }
    rfile.close();
}

Or this:

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

std::string line;
std::ifstream rfile;
rfile.open("path-to-txt-file");
if (rfile.is_open()) {
    while (std::getline(rfile, line)) {
        std::cout << line << std::endl;
    }
    rfile.close();
}
like image 99
Remy Lebeau Avatar answered Sep 20 '22 00:09

Remy Lebeau