0

Im viewing some old code that has no comments which I try to understand, the code:

std::vector<std::string> CardFactory::readFile(std::string fileName)
{
    std::vector<std::string> returnVal;
    std::ifstream myFile;
    myFile.open(fileName);
    if (myFile.is_open())
    {
        std::vector<string> textLines;
        char c[256];
        //not sure why the line below this is not in the while loop
        myFile.getline(c, 256);
        while (myFile.good()) {
            string line = string(c);
            textLines.push_back(line);
            myFile.getline(c, 256);
        }
        myFile.close();
        return textLines;
    }
    else
    {
        std::cout << "File not open " << std::endl;
        return std::vector<string>();
    }
    return returnVal;
}

It returns a vector with lines of text. I understand all the code except the part where char c is used and the value 256.

char c[256];

and

myFile.getline(c, 256);

What is the purpose of the value 256?

Sven van den Boogaart
  • 11,833
  • 21
  • 86
  • 169
  • http://en.cppreference.com/w/cpp/io/basic_istream/getline _Parameters: s - pointer to the character string to store the characters to. count - size of character string pointed to by s_ – simon Jan 20 '17 at 17:15
  • 1
    The buffersize, the amount allowed to be read. – George Jan 20 '17 at 17:15
  • 1
    Ever hear of a c style string? – NathanOliver Jan 20 '17 at 17:15
  • Note: that line is not in the `while` loop because this is a typical case of "loop-and-a-half", and people prefer copy-pasting code rather than using `break` or `goto`. – Quentin Jan 20 '17 at 17:18
  • @NathanOliver just now, if i understnad correctly an array of chars. And 256 in myFile.getline(c, 256); is the limit to reading? – Sven van den Boogaart Jan 20 '17 at 17:18
  • @Quentin I ment wouldnt it be logical to start with the myFile.getline(c, 256); in the loop. the last loop will readLine and not push it. – Sven van den Boogaart Jan 20 '17 at 17:19
  • 1
    much better would be `myFile.getline(c, sizeof(c));` – Dale Wilson Jan 20 '17 at 17:19
  • 2
    When downvoating please elaborate so i can improve the question. – Sven van den Boogaart Jan 20 '17 at 17:20
  • Yep. That is how people get input before they learn about `std::string` which is dynamic and allows you take in as much input as the user wants to supply. – NathanOliver Jan 20 '17 at 17:20
  • @Quentin A for loop might have done the trick as well. – BitTickler Jan 20 '17 at 17:20
  • @BitTickler as long as the "half" is small enough to be crammed into the `for` condition, yes. It is indeed the case here. – Quentin Jan 20 '17 at 17:22
  • @Quentin ``for(...; myFile.good(); ... )`` – BitTickler Jan 20 '17 at 17:23
  • @BitTickler that would be `for(char c[256]; myFile.getline(c, 256), myFile.good();)`. Still readable enough. – Quentin Jan 20 '17 at 17:26
  • @Quentin rather ``for(myFile.getLine(c,256);myFile.good();myFile.getLine(c,256))`` – BitTickler Jan 20 '17 at 17:28
  • @BitTickler the goal is to avoid copy-pasting the code. A "loop-and-a-half" is that common pattern when a loop has to make half an iteration at its beginning or end -- copy-pasting that half is as bad a solution as copy-pasting always is. – Quentin Jan 20 '17 at 17:32
  • 1
    `getline` returns the stream object so you could just write:`while (myFile.getline(c, 256).good())` – Ferruccio Jan 20 '17 at 17:32
  • @Ferruccio why .good()? C++ read loops since the dawn of time directly test the stream: `while(myFile.getline(...))` – Cubbi Jan 20 '17 at 18:08
  • @Cubbi - I've always done that too, but I was trying to avoid making any unwarranted assumptions. The returned stream is implicitly convertible to `bool` but if you look at the table at the bottom of this page: http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool `good()` does not behave exactly like `operator bool`. – Ferruccio Jan 20 '17 at 18:58

1 Answers1

1

The reference explains everything that you need to know. We can read that the parameters are:

Parameters

s - pointer to the character string to store the characters to

count - size of character string pointed to by s

and we can also read:

[...] extracts characters from *this and stores them in successive locations of the array whose first element is pointed to by s, until any of the following occurs (tested in the order shown):

[...]

  • count-1 characters have been extracted (in which case setstate(failbit) is executed).

So, the second argument is used to make sure that getline doesn't try to read and insert more than 255 characters into your buffer c. Inserting more than 255 characters will result in undefined behavior.

Community
  • 1
  • 1
simon
  • 2,042
  • 2
  • 20
  • 31