sábado, 16 de noviembre de 2013

Tools for I/O Streams and Characters

Tools for I/O:

Formatting Output to Files:

Using cout:

  • cout.setf(ios::fixed);
  • cout.setf(ios::showpoint);
  • cout.precision(2);

Using the out-file stream:
  • out_stream.setf(ios::fixed);              
  • out_stream.setf(ios::showpoint);      -----> show the decimal point after the decimal point.
  • out_stream.precision(2);                 -----> output of numbers with decimal points
setf is a member function of output streams, is a abbreviation for set flags (instruction to do one of two options).

Other Flags for setf are:


To create space in output we use the width functions to specifies the number of spaces for the next item. For example: 

To print the digit 7 in four spaces use:
out_stream.width(4);
out_stream << 7 << endl;

Three of the spaces will be blank.


Any flag that is set, may be unset using the unsetf function: cout.unsetf(ios::showpos);

A manipulator a is function called in a nontraditional way. In turn call members function and may or may not have arguments. We used after the insertion operator (<<).
The setw manipulator does the same task as the member function width.
To use a manupulators we use: #include <iomanip>

Streams can be arguments to a function. For example: void make_neat(ifstream& messy_file, ofstream& neat_file);

The End of The File

A way to know the end of the file is reached:
  • The boolean expression (in_stream >> next)
    • Read a value for in_stream and stores it in next
      • True if a value can be read and stored in next
      • False if there is not a value to be read (the end of the file)

Character for I/O:

Using get

char next_symbol;
cin.get(next_symbol);

Any character will be read with these statements: Blanks space and newline character ('\n').



The End of the Line
To read and echo a line of input we look first for '\n' at the end of the input line.  All characters, including '\n
 will be output.

cout<<"Enter a line of input and I will "
       << "echo it.\n";
char symbol;
do
{
      cin.get(symbol);
      cout << symbol;
} while (symbol != '\n');


'\n' - a value of type char
"\n" - a string containing only one character

In a cout-statement they produce the same result.

The Function put is a member function of every output stream.  They requires one argument of type char and they places its arguments in the output stream.  For example:

out_stream.put('A');

The putback member function places a character in the input stream.  Is useful when input continues until a specific character is read.  Places its argument of type char.

fin.putback(next);

Detecting the End of a File:

The member function eof detects the end of a file of every input-file stream. 
  • eof returns a boolean value
    • True when the end of the file has been reached
    • False when there is more data to read
  • Normally used to determine when we are NOT at the end of the file. For example:
    • if (!in_stream.eof ( ) )
To test the end of file we use two methods:
  • while (in_stream >> next)
  • while (!in_stream.eof ( ) )

Several predefined functions exist to facilitate working with caracter:

#include <cctype>

The toupper function returns the argument's upper case character:
  • toupper('a') returns 'A'
  • toupper('A') return 'A'
The tolower function retuns the argument 's lower case character:
  • tolower('A') return 'a'
The isspace function returns true if the argument is whitespace (spaces, tabs, newlines). For example:

if (isspace(next) )
    cout << '-';
else
    cout << next

Prints a '-' next contains a space, tab or newline character












No hay comentarios.:

Publicar un comentario