File Input/Output in C++
#include<fstream> //header file for file I/O
ifstream infile; //declare input and output file
ofstream outfile; // stream variables (names of your choice)
//Open the input file (associate file name and location with input file stream variable)
//Open non-existent output file or append new output to existing output file.
/*(Now use input file stream variable inFile (instead of cin) whenever input is from a file and not from the keyboard. Examples are:
infile >> value;
infile.ignore( 25, ‘$’);
Use the output filestream variable outfile (instead of cout) to output strings of text and variable values to the output file.)
infile.close(); //Close input and output
outfile.close(); // files.
Lab Assignment: Write a program to convert floating point to the nearest integer, using file input and output. The output should identify both the input value and the rounded value, in a sentence.
The input file must exist, with the data in it, before you run the program. Run twice with different input values each time. The output file should contain the results of both runs.
Lab #1: Write a C++ program which uses a void function to “flip a coin” 7000 times, and report (to a file) the number of heads and the number of tails. Be sure that running the program a second time will result in a different sequence of heads and tails.
Lab #2: (Using an eof-terminated while loop) Write a complete program to find the minimum value in a file containing an unknown number of integers. Output the result to a file.
Note: No global variables allowed! Declare filestream variables and open files in main( ).