Files Flashcards
output file
a file that data is written to
type ofstream
input file
a file that data is read from
ifstream
What three steps are taken when a file is used by a program
- Open the file 2. Process the File 3. Close the File
- Open the file 2. Process the File 3. Close the File
include
2 things to happen before data can be written to or read from a file
- a file stream object must be created
2. The file must be opened and linked to the file stream object
code for creating a filestream object and linking the file to be open to the file stream object in order to open a file for input
ifstream inputFile;
inputFile.open(“Customers.txt”)
Define an ifstream object and name it inputFile
ifstream inputFile;
Call the inputFile object’s member function and pass the string “Customers.txt” as an argument
inputFile.open(“Customers.txt”)
An example of opening a file for output writing
ofstream outputFile;
outputFile.open(“Employees.txt”);
the file stream object’s close member function to call to close a file:
inputFile.close();
Example and format of writing a string literal to a file
outputFile «_space;“I love C++ programming\n”;
Example and format of writing a string literal and the contents of a variable to a file
outputFile «_space;“Price: “ «_space;Price «_space;endl;
example reading input from a file TO THE NEXT SPACE into the variable name
inputFile»_space; name;
example reading input from a file TO THE NEXT LINE into the variable name
getline( inputfile, name)
read to the end of a file
while (!read.eof())
READING PART