PART 5
Input and Output
Standard I/O
File I/O
C++ Library Functions
STREAM INPUT
The following functions and operators are used for input from cin. The header file iostream.h must be included at the front of any program that uses these functions.
operator>
extracts the next character, string, or numeric from input
int get()
returns the next character (like C's getchar)
istream& get(char&)
extracts the next character
istream& get(char*, int n, char delim = '\n')
extracts next n characters from input
or until delimiter is reached (delimiter left in stream)
istream& getline(char*, int n, char delim = '\n')
extracts next n characters from input
or until delimiter is reached (delimiter included in string)
istream& read(char*, int n)
extracts next n characters from input
istream& ignore(int n, int delim = EOF)
discards n characters or until delimiter is reached
INPUT STREAM MANIPULATION
The following functions allow for status reporting and manipulation within the input stream. Functions that use them must include <iostream.h>
int gcount()
returns the number of characters last extracted
istream& putback(char c)
puts the last character read back
int peek()
returns the next character without extraction
istream& seekg(long n, seek_dir = ios::beg)
alters stream position to n characters from beginning (default),
current position (ios::cur), or end (ios::end)
long tellg()
returns the current stream position
istream& ws(istream&)
discards leading whitespace
STREAM OUTPUT
The following functions are used for output to cout, cerr, or clog. Functions that use them must include <iostream.h>
operator<
inserts the next character, string, or numeric to output
ostream& put(char c)
writes character c to output
ostream& write(char* buff, int n)
write n characters from array buff to output
ostream& flush()
function member to force writing of output stream
ostream& flush(ostream&)
manipulator to force writing of output stream
ostream& seekp(long n, seek_dir=ios::beg)
alters stream position to n characters from beginning (default),
current position (ios::cur), or end (ios::end)
long tellp()
returns the current stream position
ostream& endl(ostream&)
inserts a '\n' and performs a flush
STREAM STATUS
Input and output streams have a set of common functions for status setting and checking
int eof()
returns TRUE if an end-of-file was encountered
int bad()
returns TRUE if an I/O error occurred with recovery unlikely
int fail()
returns TRUE if an I/O operation or conversion failed
int good()
returns TRUE if the above three are all false
int rdstate()
returns the I/O state (0 if OK, otherwise bit map of errors)
void clear(int state = 0)
sets the I/O state (0 or ios::goodbit to clear to OK)
int operator! ()
returns TRUE if bad or fail
operator void* ()
returns FALSE if bad or fail
The status bits are as follows:
ios::goodbit0, no bits sets, all is OK
ios::eofbitat end of file
ios::failbitlast I/O operation failed
ios::badbitinvalid operation attempted
ios::hardfailunrecoverable error
testout.cpp
/* Test output formatting */
#include <iostream.h>
#include <iomanip.h> // needed for setw, setbase
main()
{
int n = 1234;
cout < "default " < n < endl;
cout.width(15);
cout < "wide? " < ":" < n < endl;
cout < "WIDE " < ":" < setw(15) < n < endl;
cout < "decimal " < n < " = octal " < oct < n
< endl;
cout < "hex " < hex < n < ' ' < n < endl;
cout < "base 7?? " < setbase(7) < n < endl;
cout < '$' < setprecision(2) < setfill('*')
< setw(20) < double(n)/100.;
return 0;
}
STREAM FORMAT CONTROL
The following functions control the formatting of input/output streams:
char fill()
returns the current fill character
char fill(char c)
sets the current fill character to c, returns previous fill character
int precision()
returns the current floating point precision
int precision(int n)
sets the floating point precision to n, returns previous value
int width()
returns the current width setting
int width(int n)
sets the width to n, returns previous width,
reset to 0 after each insertion/extraction
ios& dec(ios&)
sets decimal as the integer input base
ios& oct(ios&)
sets octal as the integer input base
ios& hex(ios&)
sets hexidecimal as the integer input base
ios& setbase(int)
sets the integer input base to any base from 2 to 16
In addition to <iostream.h>, the header file <iomanip.h> must be included to use any function whose name begins with set or reset
testout2.cpp
/* Test output formatting */
#include <iostream.h>
main()
{
double x = 1234.56789;
cout < "default " < x < endl;
cout.flags( ios::showpos );
cout < "plus sign " < x < endl;
cout.flags( ios::scientific );
cout < "scientific " < x < endl;
cout.setf( ios::showpos );
cout < "both above " < x < endl;
cout.flags( ios::uppercase|ios::scientific );
cout < "capital E " < x < endl;
return 0;
}
STREAM FORMAT STATES
Format flags are used to control the finer details of input/output formatting, such as alignment and the appearance of floating point and hexidecimal numbers
long flags()
returns the current format flags
long flags(long n)
assigns n to the format flags, returns previous format flags
long setf(long b)
turns on the specified format flag(s),
returns previous format flags
long setf(long b, long c)
clears the format flags(s) in c, turns on the format flags(s) in b,
returns previous format flags
long unsetf(long b)
clears the format flag(s) in b, returns previous format flags
FORMAT FLAGS
The following flags are used to control I/O formatting. The actions described below are for the bits being set. The opposite action is implied for bits not being set.
adjustfield group
left / assumes left adjusted, padding on rightright / assumes right adjusted, padding on left
internal / assumes padding internal, between sign and value
basefield group
showbase / output preceding 0 for octal, 0x or 0X for hexshowpoint / output decimal point and trailing zeroes for floating-point
showpos / output preceding + sign for positive integer
floatfield group
scientific / output floating point in scientific notationfixed / output a fixed number of digits after decimal point
(not in a group)
uppercase / use X and E for hex and scientific notationskipws / skips white space on input
FILE I/O
File I/O in C++ is done by associating the file with an input, output, or input/output stream using a variable of the ifstream, ofstream, or fstream class. To perform file I/O the header file <fstream.h> must be included.
Opening a file for input only or output only can be done simply with a constructor function as follows:
ifstream inFile("input.dat");
// opens existing input.dat for reading
ofstream outFile("output.dat");
// opens new output.dat for writing
ofstream appFile("log.dat", ios::app);
// opens existing log.dat for appending
Once this association is made, all input and output can be done using the stream functions already described.
Stream input and output from files should always be checked for errors.
This is done simply as follows
if (!inFile)// open failed
OPENING FILES
Establishing an input, output, or input/output stream for files can also be done as two statements, one to construct the stream variables, and another to open the files
ifstream inFile;
ofstream outFile, appFile;
fstream randomFile;
...
inFile.open("input.dat");
outFile.open("output.dat");
appFile.open("log.dat", ios:app);
randomFile.open("database.dat", ios::in|ios::out);
The general form of the open statement is:
void open(char* name, int mode = ios::out, int prot = filebuf::openprot)
(The protection mode is operating system dependent.)
This opens the named file in the indicated access mode.
Valid modes are
ios::app / for appended outputios::ate / seek end-of-file at open
ios::in / for input
ios::out / for output
ios::trunc / discard previous contents of the file
ios::nocreate / file must exist
ios::noreplace / file cannot exist
CLOSING A FILE
void close()
Closes the file, and disassociates the stream from the file. For example,
inFile.close();
outFile.close();
Mixing C Style and C++ Style I/O
If you have a program that attempts to use BOTH the C-style functions that use stdin AND cin in C++ you could, in general, have great difficulty in getting the program to work. stdin and cin are, in general, two different buffers using different buffering schemes. If you wish to use both, you must call cin.sync_with_stdio(); This will slow performance.
Likewise, if you have a program that attempts to use BOTH stdout in C AND cout, you can do one of the following:
a. call cout.sync_with_stdio();
or
b. 1. between each switch from the use of cout to stdout, use the endl function/manipulator or the flush function/manipulator
and
b. 2. between each switch from the use of stdout to cout, use the fflush function
fileio2.cpp
/* Program to copy one file to another. */
#include <iostream.h> //for cin, cout, cerr
#include <fstream.h> //for ifstream, ofstream
#include <stdlib.h> //for exit
main()
{
char in_name[25], out_name[25];
ifstream in_file;
ofstream out_file;
char c;
cout < "Source file??\n";
cin.width(sizeof(in_name));
cin > in_name;
cout < "Destination file??\n";
cin.width(sizeof(out_name));
cin > out_name;
in_file.open(in_name, ios::in);
if (!in_file)
{
cerr < "Can't open source file.\n";
exit(1);
}
out_file.open(out_name, ios::out);
if (!out_file)
{
cerr < "Can't open destination file.\n";
exit(1);
}
/* this is the main loop */
while ( in_file.get(c) )
out_file.put(c);
in_file.close();
out_file.close();
cout < "Finished!!\n";
return 0;
}
reverse3.cpp
/* This programs reads a file whose name is entered on the
command line and prints the file in reverse order to
<stdout>. This program works only on selected file
organizations. It will generally work on files
written by a C program (such as fileio2.cpp) */
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
long n;
if ( argc != 2)
exit(1);
ifstream infile(argv[1]);
if ( !infile )
exit(1);
infile.seekg(-1L, ios::end);
/* position at last byte of file */
n = infile.tellg();/* n + 1 = file size */
while ( n-- >= 0 )
{
cout < char(infile.get());
infile.seekg(n, ios::beg);
}
cout < endl;
return 0;
}
Lab 1 for Part 5
1. Experiment with various formatting options using stream I/O, including the printing of preceding zeroes.
2. Write a program that determines the size of a file by reading and counting every character in the file.
3. Write a function that compares two files, finding either the line number and character position of the first difference between the two files, or verifying that the two files are character-for-character identical. Print the line number and character position of the first difference between the two files.
Input and OutputPart 5, Page 1