C++

Objectives : Showing how to terminate an application using cin.fail() in a while statement block, to handle the errors like buffer-overflow during an input operation.

  • File Name : char_array_getline1.cpp
    • Syntax : cin.getline(cstr1, 12);//extracts characters into C-string
    • An oversized data to function cin.getline(s1, 11), causes stack overflow, raises fail-state flag of iostream,  and the function ios::cin.fail() suspendedthe subsequent  streaming operations.
    • Other functions:
      •  cin.getline(cstr1, 12);//extracts characters into C-string
         cin.ignore( 12, \n );//extracts and discards 12 character.
         
  • Code: char_array_getline1.txt, char_array_getline2.txt
 

Step: 1 Create a source file.

Step: 2 Edit and save Source file

Step: 3 Runtime Views: In this example, the application will quit on buffer-overflow. The "while statement block" has a parameter-constraints on input streaming, that if console-input (cin) gets an oversized string, it will generate a fail-bit and as a result, the application will just quit after displaying the data stored in the stack.

a) Testing while loop : Note as long as you enter a string containing less than 11 characters, the application will produce expected result.

To terminate the code, you will have to feed oversized data.

b) Testing cin.getline(ch1, 11) tolerances: Enter a line containing more than 11 characters.

Note, all instructions in "While" statement were ignored.

 

Step: Brief discussion:

Prototype: void cin.getline(char *ch1, const int size);

In the previous examples, namely cin_stream1.htm and gets_unsafe1.htm,  we understood the error handling schemes of iostream libraries of C++, especially handling stack-buffer overflow. Some functions are globally persistent, and others are volatile, meaning that these functions work as tributaries to the persisted functions.

  Here in this example we took a closer look at ""cin.fail()" function. Create another source file, as shown below. The first illustration shows the code snippet in main function, and second illustrations shows the code snippet of "cinchcks" function.

 

The codes in cinchecks function.

Run this application and type a short  string to avoid buffer overflow. Then at the next prompt enter an oversized string, as indicated below.

The oversized data raised a fail flag, but the data was not bad. The error could be cleared out with "cin.clear"  function. The function "cin.ignore", used in the above example, would ignore the stream (emptying  the buffer)  and prompt another chance to the user to enter another string.

As long as the user, enters correct size of data, there won't be any error .


 

The function, cin.getline(cs1, 12), extracts characters from the input sequence and stores them as a c-style-string into the array and terminated by a null character. If the user enters more than size-1 characters, the function raises flag, "failbit" and terminate the operation. This will prevent the system from holding up or crash.

It appears that cin.fail() is a whistle blower of iostream, and raises the flag once with a cin.failbit, and cin.badbit. The flag, failbit, is raised from an internal error, can be cleared up for succeeding operations. But the flag, "badbit" is only raised due to bad-stream or corrupted stream, and
suspends other operations. Please visit (ios_failbit1.htm )

Special Introduction: iostream inheritance structures

The class iostream inherits many important functions from other classes,  some of those functions are mentioned below.

  • function inherited from istream
    • cin.getline() : is used with input streams, reads characters into a buffer till one of the following conditions is raised.
      • A new line flag is raised.
      • End of the file (EOF)  or a delimiter syntax is
    • >> operator : input/extraction operators.
    • The other functions like get, ignore, read  and peek are inherited from istream class.
  • functions inherited from ostream:
    • << operator : output/insertion operators.
    • The functions like put, write, flush and others, are inherited from ostream.
  • functions inherited from ios:
    •  good : checks if the stream is good.
    • bad : checks if badbit is set,
      • syntax : bool bad();
      • The cin.bad() function returns true(1) if a fatal error with the current stream has occurred, false(0) otherwise.
    • eof : checks eofbit. (eof:end of the the file)
    • fail : cheks failbit (an error in the stream) or badbit ( a fatal error is encountered).
      • The syntax, "cin.setstate(ios::failbit) " is used to set the "State To Fail".
    • rdstate(): checks on the status of the current stream with the flags like eofbit, failbit and badbit. The ground state of this function is set at 0, and with an input  extraction error (cin.gelline(ch1,11)", the value of "cin.rdsate()" is set to 4 , and for file the value is set to 5.  ( cin_rdstate1.htm )
      • The syntax cin.rdstate()