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:
why is the number needed in the
getlinemethod?does
linehave\0at the end automatically? because when we createcharvars 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 thejxxxlines? (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?
3 Answers
you are calling
std::ifstream::getline(), which takes achar*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 changelinetostd::stringand usestd::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 bygetline(), 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. Usewhile (rfile)instead. Or better,while (rfile.getline(line, 20)). Either way will account for any errors that occur in bothopen()andgetline(), but the latter ensures thatcoutis not called ifgetline()fails.cout >> data >> endl;is also wrong. You need to use<<withstd::cout, anddatashould belineinstead.
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(); } 2- The number you are providing there is the maximum number of char to read.
- The final character in ifstream.getline, which you are using, is a \0
- It will return at either when the number of chars is reached or a newline
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
- getline() function takes a pointer to char as a parameter, it has no way to know how big the array is, that's why you have to pass the number yourself, otherwise it could try to read chars into memory "outside" of the array.
See Passing arrays to function in C++
- Char arrays are not null terminated by default, they must be done so explicitly (like getline() will do here), but you need it only if you want to use your char array as a string. This way you can write some function that takes char* as parameter without additional one with the size of the array, you will know where the end of the string is when you encounter 0 even if the caller doesn't pass the size of it like you would for getline() in your code. This is pretty much the idea behind c-style string.