i wanna read all the lines from a text so am doing this
int main(){
fstream fs("test.txt",fstream::in|fstream::ate);
int length = fs.tellg();
std::vector<char> buffer(length);
fs.seekg(0, ios::beg);
fs.read(buffer.data(), length);
int newlen= 0;
int ptrSeek = 0;
while(buffer.data()[ptrSeek] != 0){
ptrSeek++;
newlen++;
if(ptrSeek == buffer.size()) { break;}
}
std::vector<char> temp(newlen,0);
memcpy(&temp[0],&buffer[ptrSeek-newlen],newlen);
}
test.txt:
this is a test
this is a test
so when it reads it, it reads it like this
[t] [h] [i] [s] [ ] [i] [s] [ ] [a] [ ] [t] [e] [s] [t] [ ] [t] [h] [i] [s] [ ] [i] [s] [ ] [a] [ ] [t] [e] [s] [t]
how can i know it start reading from the next line?
You can check against
\nto know if the character is a newline.However, in your case, I would suggest you to high-level function, such as
std::getlinewhich reads a single line at a time, saves you much of the labor you’re doing manually.The idiomatic way to read line would be as: