MT 262 - Block 2 - Unit 4 - C++ Files
fellstrider.com - the logo!
Home| OU Study Rooms | MT262 Index | Block 2 - Structures
 
C++ Files

Files

In C++ the library that holds the files is known as the file stream library. (#include <fstream.h>) The varying operating systems can handle certain features differently. Permitted file names vary. The 8 + 3 convention is normally safe to use:

Permitted characters:

Upper and lower case are regarded as the same. Embedded systems can be more restrictive.

The fstream library contains several classes including:

The library iostream becomes available following #include <fstream.h>. This contains the cout file for writing to the screen and cin file for reading from the keyboard. Opening and closing of these files is automatic.

Insertion operator <<

int main(int argc, char* argv[])
{ //demonstrating the insertion operator <<
  cout << "File stream input/output\n";   //Cursor moved to next line by \n
  cout << 12345;
  cout << true;   //returns a 1
  cout << 12345 << " File stream input/output\n";   //Combining insertions
  getchar();
        return 0;
}

Extraction operator >>

int main(int argc, char* argv[])
{
int Integer;
float Float;
  cout << "File stream input/output - input\n";
  cout << "Enter an integer, then press Enter: ";
  cin >> Integer;  //reads in integer from keyboard
  cout << "\nYou entered: " << Integer << "\n";

  getchar();
        return 0;
}

Insertion/Extraction and strings

These present problems with string:

Strings containing spaces cannot be read and stored using this method.

Home| OU Study Rooms | MT262 Index | Block 2 - Structures
Move on to Block 3 - Visual programming

Valid CSS! Valid XHTML 1.0!

Comments, suggestions, ideas to
Stuart Banner