Next: Summary Up: Strings in C++ Previous: String Output

String Input

When the input stream cin is used space characters, newline etc. are used as separators and terminators. Thus when inputting numeric data cin skips over any leading spaces and terminates reading a value when it finds a white-space character (space, tab, newline etc. ). This same system is used for the input of strings, hence a string to be input cannot start with leading spaces, also if it has a space character in the middle then input will be terminated on that space character. The null character will be appended to the end of the string in the character array by the stream functions. If the string s1 was initialised as in the previous section, then the statement

cin << s1;
would set the string s1 as follows when the string "first" is entered (without the double quotes)
|f|i|r|s|t|\0|e|\0|
Note that the last two elements are a relic of the initialisation at declaration time. If the string that is entered is longer than the space available for it in the character array then C++ will just write over whatever space comes next in memory. This can cause some very strange errors when some of your other variables reside in that space!

To read a string with several words in it using cin we have to call cin once for each word. For example to read in a name in the form of a Christian name followed by a surname we might use code as follows:

char christian[12], surname[12];
cout << "Enter name ";
cin >> christian;
cin >> surname;
cout << "The name entered was "
     << christian << " "
     << surname;

The name would just be typed by the user as, for example,

Ian Aitchison
and the output would then be
The name entered was Ian Aitchison

In Lesson 19 it was noted that it would be useful if the user of a program could enter the name of the data file that was to be used for input during that run of the program. The following example illustrates how this may be done. It assumes that a file name for an input file must be entered and also a file name for an output file.

// IEA 1996
// Example program which copies a specified
// input file to a specified output file.
// It is assumed that the input file holds a
// sequence of integer values.

#include <iostream.h>
#include <fstream.h>

int main()
{
  ifstream ins;                  // declare input and output
  ofstream outs;                 // file streams
  char infile[20], outfile[20];  // strings for file names
  int i;

     // ask user for file names
  cout << "Enter input file name: ";
  cin >> infile;
  cout << "Enter output file name: ";
  cin >> outfile;

     // Associate file names with streams
  ins.open(infile);
  if (ins.fail())
    {
      cout << "Could not open file " << infile
           << " for input" << endl;
      return 1; // exit with code 1 for failure
    }
  outs.open(outfile);
  if (outs.fail())
    {
      cout << "Could not open file " << outfile
           << " for output" << endl;
      return 1; // exit with code 1 for failure
   }

     // input from input file and copy to output file
  ins >> i;
  while (!ins.eof())
    {
      outs << i << " ";
      ins >> i;
    }
  outs << endl;

      // close files
  ins.close();
  outs.close();
  return 0; //return success indication.
}
Download program.
This program assumes that the file names entered by the user do not contain more than 19 characters. Note how a space character was output after each integer to separate the individual values in the output file.



Next: Summary Up: Strings in C++ Previous: String Output