Using an IFSTREAM Object with the ">>" Operator

ID: Q92733


The information in this article applies to:

An IFSTREAM object may be used as input for the ">>" insertion operator. Doing so provides a method to retrieve data from an IFSTREAM other than CIN. The sample code below demonstrates this task.

Sample Code


/*
 * Compile options needed: cl /D_DOS test.cpp safxcrd.lib
 */ 

//****************************************************
// 
//      Demo using an IFSTREAM Object For >> operator
// 
//****************************************************

#include <fstream.h>
#include <iostream.h>
#include <malloc.h>
#include <string.h>

ifstream myfileinput;               // IFSTREAM Object For >> Operator

void main()
{
   int     number;
   char    *instring;

   myfileinput.open("my.dat", ios::in, filebuf::sh_read);

   myfileinput >> number;      //** Insert into number

   if (number == 180)
      cout << "worked for integers" << endl;
   else
      cout << "failed for integers" << endl;
   instring = (char*)calloc(25, sizeof(char));

   myfileinput >> instring;   //** Insert into char*

   if (0 == strcmp(instring,"string"))
      cout << "worked for strings" << endl;
   else
      cout << "failed for strings" << endl;
} 

MY.DAT File


180
string 

Output


worked for integers
worked for strings 

Additional query words: kbinf 7.00 1.00 1.50


Keywords          : kb16bitonly 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: July 27, 1999