Input and Output in C++

Introduction

We have covered the use of file streams in Pascal. In C++, the insertion operator << puts data into a stream and the extraction operator >> reads data from a stream. The standard stream for keyboard input is cin and that for output to the console is cout.

Output to the Console

This is the equivalent of our C output demonstration.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
  int my_int = 42;
  float my_float = 1.2345f;
  char my_char = 'a';
  string my_string = "This is my string.";
  cout << "my_int: " << my_int << endl;
  cout << "my_float to 3 dp: " << fixed << setprecision(3) << my_float << endl;
  cout << "my_char: " << my_char << endl;
  cout << "my_string: " << my_string << endl;
  cout << "words\tseparated\tby\ttabs\n" << endl;
  cout << "\a"; // beep

  return 0;
}    

Output:

my_int: 42
my_float to 3 dp: 1.235
my_char: a
my_string: This is my string.
words   separated       by      tabs

Input from the Keyboard

This is the equivalent of our demonstration of input from the keyboard in C.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
  string forename;
  char character;
  int age;
  float float_input;
  cout << "Please enter your first name: ";
  cin >> forename;
  cout << "Hello, " << forename << endl;
  cout << "Please enter a character: ";
  cin >> character;
  cout << "Character entered: " << character << endl;
  cout << "Please enter your age: ";
  cin >> age;
  age++; // add 1 to age
  cout << "Your age next year: " << age << endl;
  cout << "Please enter a float such as 1.2345: ";
  cin >> float_input;
  cout << "Your float to 3dp: " << fixed << setprecision(3) << float_input;

  return(0);
}
    

Input and output:

Please enter your first name: Joe
Hello, Joe
Please enter a character: a
Character entered: a
Please enter your age: 17
Your age next year: 18
Please enter a float such as 1.2345: 1.2345
Your float to 3dp: 1.235

Output to a Text File

This cplusplus page on files is most useful. Our equivalent of the C example, prints ten lines to a text file. Here we open a file for writing only and in the following section we open the file with read access.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
  ofstream ofs;
  ofs.open("test.txt");
  for (int i = 1; i <= 10; i++)
    ofs << "line " << i << "\n";
  ofs.close();
  
  return 0;
}
    
In order to append data instead of rewriting the file, open instead with this line of code:
ofs.open("test.txt", ios::app);    

To test this, execute the program twice then open with a text editor test.txt (which you will find in the project folder).

Input from a Text File

This demonstration, the equivalent of our C example, requires in its project folder a copy of the text file from the first version of the previous program.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
  string line;
  string lines[10];
  int i = 0;
  ifstream ifs;
  ifs.open("test.txt");
  while (getline(ifs, line))
  {
    lines[i] = line;
    i++;
  }
  ifs.close();
  // Print alternate lines in array
  for (int i = 0; i < 9; i += 2)
    cout << lines[i] << "\n";

  return 0;
}

Output:

line 1
line 3
line 5
line 7
line 9

Writing to and Reading from a Binary File

The parameters for the read and write methods are (1) a pointer to char and (2) the number of bytes in the block to be transferred. We cast each integer and float address to a char pointer.

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
  int myInt = 5;
  float myFloat = 3.142;
  int myIntFromFile;
  float myFloatFromFile;
  ofstream ofs;
  ofs.open("test.dat", ios::binary);
  ofs.write((char*) &myInt, 4);
  ofs.write((char*) &myFloat, sizeof(float));
  ofs.close();

  ifstream ifs;
  ifs.open("test.dat", ios::binary);
  ifs.read((char*)&myIntFromFile, 4);
  ifs.read((char*)&myFloatFromFile, sizeof(float));
  ifs.close();
  cout << "integer read from binary file: " << myIntFromFile << " " << endl;
  cout << "float read from binary file: " <<  fixed << setprecision(3) <<  myFloatFromFile << endl;

  return 0;
}

Output:

integer read from binary file: 5
float read from binary file: 3.142
Programming - a skill for life!

How to apply your knowledge of Pascal when learning C/C++