-4
#include<iostream>
#include<fstream>
using namespace std;
void main()
{
  char word[5];
  ifstream file;
  file.open("abcd.txt",ios::in);
  if(file)
  {
    while(file>>word)
    {
       cout<<word<<" ";
    }
  }
  else
    cout<<"file not opened";
}

CONTENT OF THE FILE abcd.txt:

pineapple orange seventeen computer

The output which i get is:

pineapple orange seventeen computer

I have mention the array size as 5 but the extraction(>>) operator read words with size more than five into the array (but the array size is five!).How do this happens?.How is this possible?

The output which i expected is:

pinea orang seven compu
Guhan
  • 97
  • 9
  • 4
    Undefined behaviour. The extraction operator doesn't know what the size of the input buffer is, and so cannot limit the size. –  Jun 16 '18 at 17:06
  • why does it happens – Guhan Jun 16 '18 at 17:07
  • Because that's the way C++ is defined to work, or not work, in this case. –  Jun 16 '18 at 17:08
  • 1
    It happens because of a programmer error. Probably should use `std::string` instead of a fixed length buffer of insufficient size. – Eljay Jun 16 '18 at 17:12
  • 1
    The Why: A) `>>` is a function. When you pass an array into a function it decays to a pointer (Read [What is array decaying?](https://stackoverflow.com/questions/1461432/what-is-array-decaying) for more on that). Since the buffer is now a pointer its size has been lost. B) `>>` is designed to read into a char pointer reads until it finds whitespace. C) C++ follows a philosophy of not making programs pay for what they don't need. Testing at runtime for programmer error is a cost a properly written program should not have to pay. – user4581301 Jun 16 '18 at 17:24
  • This doesn't address the question, but `ifstream file; file.open("abcd.txt",ios::in);` can be (and should be) `ifstream file("abcd.txt", ios::in);`. Get in the habit of using constructors to initialize objects. Also, you don't need the second argument; that's the default value. `ifstream file("abcd.txt");` does exactly the same thing. – Pete Becker Jun 16 '18 at 17:25

1 Answers1

1

As Neil Butterworth mentioned, the >> operator can't know when the char array is too small for the input, and therefore might try to write past the end of the array.

Writing past the end of an array causes Undefined Behavior. This means that the C++ standard does not say what should happen if you try to do this. Your program could crash, but it's not guaranteed. The memory after the char array could have also been used for something else, which would make your program behave very weirdly and be hard to debug. When you have undefined behavior, anything could have happened.

In this case, the program seems to have "worked" by plain luck because the memory after the array is unused. However, if you have compiled it with a different compiler or executed the program in a different environment, one of the things I mentioned above might have happened instead. In case it isn't clear yet, you should never have undefined behavior in your programs.

eesiraed
  • 4,626
  • 4
  • 16
  • 34