Search code examples
c++fstream

C++ fstream getline parameters


I am new to C++ and I want to ask a question about how to find a line in a file using fstream. I only found this and would someone explain to me what these parameters mean?

file.getline(char *,int sz);

Thanks


Solution

  • If you mean std::basic_stream::getline(), you provide a pointer to character array and the size of that array. You have to create the array somewhere by yourself. If some line is longer than sz - 1, only part of it with length sz - 1 will be read.

    If you don't know the maximum length of lines in the input file, it's better to use std::getline(), for example like this:

    std::string line;
    std::getline(file, line);