cin(standing for "common input"). Its use is similar to that of the cout function but with the input operator >>.
cin statements only contain a list of variables (separated by >>) which need new values from outside the program.
NOTE CAREFULLY WHAT THIS FUNCTION DOES!!! cin with the >> operator takes values from outside the program (from the keyboard or an independent data file) into variables internal to the program.
These functions are like the BASIC language INPUT statement and NOT like the BASIC language READ. The BASIC READ needs a BASIC DATA inside the program. There is nothing like that in C++ (or Pascal or FORTRAN).
NOTE: the values of the variables listed in a cin statement are changed!
The type of the input data must correspond to the types of the variables in the list. (The exception is that integers can be read into real variables.) In the input, spaces indicate the end of one numeric input.
cin ignores "white space," i.e., blanks, tabs, newlines.
If there are not enough data on one input line to correspond to the number of variables listed, cin will search for appropriate data on the next input line. Data on a line that remains unread by one cin statement will be available for reading by the next cin statement.
There is a way to imitate the Pascal readln or Fortran READ statement in C++ using stream i/o, but that will be left until later. These statements will only read a specified number of data items on an input line and ignore whatever else remains on the line.
Suppose the input was
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16Suppose the variables a, b, c, d, and e are all declared as integers.
PROGRAM SEGMENT
cin >> a >> b >> c ; cin >> d ; cin >> e ; Variables: a b c d e Values: __ __ __ __ __
math 68: prog2 < input.file > output.file
where the (executable) program name is prog2 and the < sign
specifies that the filename that follows is to be used for
input rather than the default (keyboard) and the > sign specifies
that the filename that follows is to be used for output rather than
the default (monitor). A similar functionality is possible on the
DOS input line on IBM-compatible PCs.
On the Santa Clara University DEC Alpha, the special command file EXCXX.COM (created for CS courses in the Math Dept and available in FACULTY:[DSMOLARSKI.MA10]) imitates this "runtime i/o file redirection."
However, on other systems and for certain applications, one might want the program itself somehow to know which file is to be used for input and which for output.
To do this one declares certain identifiers to refer to symbolic file names, then associates these symbolic file names with actual disk file names by a program command, and finally uses these symbolic file names in a way similar to the way cin and cout are used.
To make use of this feature of C++, one adds a new header include statement:
#include <fstream.h>where fstream abbreviates "file-stream."
fstream includes two types: ifstream (input-file-stream) and ofstream (output-file-stream). ifstream and ofstream can be used (in the same way as other type name, e.g., int) to declare identifiers of that type. For example,
ifstream InFile; ofstream OutFile;declares that the identifier InFile is of type ifstream and thus can be used to refer to some data file that will be used for input data. Similarly with OutFile and output data.
To associate the identifier InFile with an actual data file (similar to giving it a specific "value"), and to prepare the file for reading or writing, one uses the open function which C++ makes part of the types ifstream and ofstream and any variable that is declared of either type. This is done in the following way:
InFile.open("a:in.dat");
This statement uses the open function which is part of the
identifier InFile to open the datafile in.dat on
disk drive a:. (On "math" or on the Alpha, one would not need to
specify the disk drive.) "Opening" a file means preparing it to be
read from (or written to) and also (in this case) making the association
between the filename the disk knowns (in.dat)
and the symbolic name that the program will use (InFile).
Within the program in.dat is never again used.
Within the program, after this open statement is executed, the symbolic name InFile refers to the physical file in.dat on drive a and is used as cin would be used.
Right before the return statement, one needs to "close" any file opened by the program. On many systems, if an open output file is not explicitly closed, any data written to the file will be lost. This is done by the following statement:
InFile.close();which makes use of the function close which is part of the data types ifstream and ofstream and thus part of any identifier declared to be of either type. This function closes the actual file associated with the symbolic name (in this case the disk file "in.dat" associated with InFile earlier). (Because of the earlier association between the actual filename and the symbolic name, there is no need to include the actual filename in the parentheses again and so nothing is put there.)
SAMPLE PROGRAM
#include <fstream.h>
#include <iostream.h>
int main()
{ int n1, n2, n3;
// declaring and opening InFile=in.dat
// and OutFile=out.dat
ifstream InFile;
ofstream OutFile;
InFile.open("in.dat");
OutFile.open("out.dat");
// reading from in.dat and writing to out.dat
InFile >> n1 >> n2 >> n3 ;
OutFile << n1 << ' ' << n2 << ' ' << n3 << endl;
InFile >> n1 >> n2 >> n3 ;
OutFile << n1 << ' ' << n2 << ' ' << n3 << endl;
InFile.close();
OutFile.close();
return 0;
}
Using this approach to input and output is often the best way to proceed with
programs on personal computers.
BEWARE: By default, you will not be given any error message if the actual disk name associated with an input file symbolic identifier does NOT exist! One can use a function to test whether an error has occurred or not, but be aware that if a program does not seem to be reading data from an input file correctly, it could be because the actual name used in the program code in the open statement does not correspond to anything on the disk!
If only the first part of the data present in an input line is desired, it is possible to disregard the remaining data by using the ignore function which is part of cin or any internal file variable. This function take two arguments, the first one indicating the maximum number of characters to ignore next, and the second one indicating a character at which to stop disregarding input stream characters (it also ignores that particular character). (If the second argument is missing, the function will cause the designated number of characters to be disregard no matter what characters it contains.)
Thus,
cin.ignore(5,'A');
will ignore the next five characters in the input stream or all
the characters (within the first 5) up to and including the character
'A'.
cin.ignore(5);
will ignore the next five characters no matter what is contained
among those characters.
To ignore whatever remains on the input line, one can use
cin.ignore(100,'\n');
which prescribes that the code should ignore the next 100 input
characters or all characters up to and including the newline symbol
(i.e., '\n'). (One can use any large number in place of 100, but
100 is often large enough for most input lines, but not overly large.)
This function effectively does the same thing that a Pascal
readln; command does.
If the code includes a input file definition, such as
ifstream Infile;
then the code can make use of the command Infile.ignore in
a similar way.
INPUT FILE
7 8 9 10
10 11 12 13
1 2 3 4
PROGRAM SEGMENT
{ ifstream Infile;
int a,b,c;
...
Infile >> a >> b >> c ;
Infile.ignore(100,'\n');
a = b + c + a;
cout << a << endl;
Infile >> a >> b >> c ;
Infile.ignore(100,'\n');
cout << a << endl;
a = b + c;
cout << a << endl;
Infile >> c >> b >> a ;
Infile.ignore(100,'\n');
cout << a << endl;
a = a + b;
cout << a << endl;
}
ANALYSIS
variables: a b c values:OUTPUT
| | | v v v
This page is maintained by Dennis C. Smolarski, S.J.
dsmolarski@math.scu.edu
© Copyright 1997, 1998, 1999, Dennis C. Smolarski, SJ, All rights reserved.
Last changed: 10 January 1999.