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 createchar
vars likechar text[5]; text = "abc1"
, it is actually saved as"acd1\0"
and what happens to the endlines in the file (\n
) after each of thejxxx
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?