The char_traits, not_eof(), to test whether a character is not the end-of-file (EOF) character or vice versa C++ code sample

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the not_eof () to test whether a character is not the end-of-file (EOF) character or is the EOF in C++ programming

To show: How to test whether a character is not the end-of-file (EOF) character or is the EOF using not_eof() C++ programming

 

// using the char_traits, not_eof() testing the EOF

#include <string>

#include <iostream>

using namespace std;

 

int main(void)

{

char_traits<char>::char_type chr1 = 'w';

char_traits<char>::int_type int1;

 

int1 = char_traits<char>::to_int_type(chr1);

cout<<"Operation: to_int_type(chr1)"<<endl;

cout<<"The char_type "<<chr1<<" = int_type "<<int1<<endl;

 

// EOF

char_traits <char>::int_type int2 = char_traits<char>::eof();

cout<<"\nOperation: char_traits<char>::eof()"<<endl;

cout<<"The eof return is: "<<int2<<endl;

 

// testing for EOF

char_traits <char>::int_type eofTest1, eofTest2;

eofTest1 = char_traits<char>::not_eof(int1);

cout<<"\nOperation: not_eof(int1)"<<endl;

 

if(!eofTest1)

cout<<"The eofTest1 indicates "<<chr1<<" is an EOF character."<<endl;

else

cout<<"The eofTest1 returns: "<<eofTest1

<<", which is the character: "

<<char_traits<char>::to_char_type(eofTest1)<<endl;

 

eofTest2 = char_traits<char>::not_eof(int2);

cout<<"\nOperation: not_eof(int2)"<<endl;

 

if(!eofTest2)

cout<<"The eofTest2 indicates "<<chr1<<" is an EOF character."<<endl;

else

cout<<"The eofTest1 returns: "<<eofTest2

<<", which is the character "

<<char_traits<char>::to_char_type(eofTest2)<<endl;

 

return 0;

}

 

Output example:

 

Operation: to_int_type(chr1)

The char_type w = int_type 119

 

Operation: char_traits<char>::eof()

The eof return is: -1

 

Operation: not_eof(int1)

The eofTest1 returns: 119, which is the character: w

 

Operation: not_eof(int2)

The eofTest2 indicates w is an EOF character.

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index