Using an IFSTREAM Object with the ">>" Operator

Last reviewed: July 17, 1997
Article ID: Q92733
1.00 1.50 WINDOWS kbprg

The information in this article applies to:

  • Microsoft C/C++ for MS-DOS, version 7.0
  • Microsoft Visual C++ for Windows, versions 1.0 and 1.5

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 reference words: kbinf 7.00 1.00 1.50
KBCategory: kbprg
KBSubcategory: CPPLngIss
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.