Reputation: 121
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 x
s 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:
why is the number needed in the getline
method?
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)
Will the program move to the next line automatically? If not, how do I tell it to go to the next line?
Upvotes: 12
Views: 135155
Reputation: 74
See Passing arrays to function in C++
Upvotes: 0
Reputation: 596246
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.
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.
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();
}
Upvotes: 22
Reputation: 1688
It sounds like you want the getline function that would be used like getline( stream, &string, delemiter ), so in your case
std::string buff;
getline( rfile, buff )
This will read the file until a newline is found and store the string, minus the newline, in buff
Upvotes: 0