You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Input File Stream. This data type can be used only to read data from files into memory.
ofstream
Output File Stream. This data type can be used to create files and write data to them.
fstream
File Stream. This data type can be used to create files, write data to them, and read data from them.
Input Stream – data stream from which information can be read
Ex: cin to read from the keyboard
Ex: ifstream objects to read data from files
Output Stream – data stream to which information can be written
Ex: cout to write to the computer screen
Ex: ofstream objects to write data into files
2. File Access (Mode) Flags:
File Access Flag
Meaning
ios::app
Append mode. If the file already exists, its contents are preserved and all output is written to the end of the file. By default, this flag causes the file to be created if it does nor evist.
ios::ate
If the file already exists, its contents are preserved and all output is written to the end of the file.
ios::binary
Binary mode. When a file is opened in binary mode, data are written to or read from it in pure binary format. (The default mode is text.)
ios::in
Input mode. Data will be read from the file. If the file does not exist, it will not be created, and the open function will fail.
ios::out
Output mode. Data will be written to the file. By default, the file's contents will be deleted if it already exists.
ios::trunc
If the file already exists, its contents will be deleted (truncated). This is the detault mode used by ios::out.
Opening a File with the File Stream Object Definition Statement:
This statement defines a stream object named dataFile and uses it to open the file names.txt.
The file is opened in both input and output modes. This technique eliminates the need to call the open function when your program knows the name and access mode of the file at the time the object is defined.
Opening Files with Constructors
ifstream, ofstream, and fstream have initialization constructors that take a filename (as a C-string) and an open mode. C++ 11: The filename can be a string object.
Stream constructors have overloaded versions that take the same parameters as open
These constructors open the file, eliminating the need for a separate call to open
fstream inFile("myFile.dat", ios::in);
3. Examples
3.1
Write a complete program that reads integers from a file called numbers.txt, and displays their sum on the screen.
If the content of numbers.txt is: 10 20 30 100
The program should print: 160
#include<iostream>
#include<fstream>usingnamespacestd;intmain() {
int num;
int sum = 0;
// ifstream inputFile;
fstream inputFile;
// inputFile.open("numbers.txt");
inputFile.open("numbers.txt", ios::in);
while(inputFile >> num)
sum += num;
cout << sum;
inputFile.close();
return0;
}
3.2
Write a complete program that uses an ifstream object to read integers from a file called numbers.txt, and an ofstream object to write into a file called doubles.txt.
Your program should have a loop that reads five integers from the input file, then writes the double of each (×2) into the output file. Write each value in a separate line.
#include<fstream>usingnamespacestd;intmain() {
int num;
int doubleNum;
ifstream inputFile;
ofstream outputFile;
inputFile.open("numbers.txt");
outputFile.open("doubles.txt");
for (int i = 0; i < 5; i++) {
// Input (read) data from the file into the variable `num`.
inputFile >> num;
doubleNum = num * 2;
// Output (write) data from the variable `doubleNum` into the file.
outputFile << doubleNum << endl;
}
inputFile.close();
outputFile.close();
return0;
}
3.3
Write a complete program that uses an ifstream object to read integers from a file called numbers.txt, and an ofstream object to write into a file called evens.txt.
Your program should have a loop that reads each integer from the input file, then writes it into the output file if it is an even number. Write each even number in a separate line.
#include<fstream>usingnamespacestd;intmain() {
int num;
// ifstream object to read data from file.
ifstream inputFile;
// ofstream object to write data into file.
ofstream outputFile;
inputFile.open("numbers.txt");
outputFile.open("evens.txt");
// Read data from the file (into the variable `num`).while(inputFile >> num) {
if (num % 2 == 0) {
// Write data into the file.
outputFile << num << endl;
}
}
inputFile.close();
outputFile.close();
return0;
}
The text was updated successfully, but these errors were encountered:
1. File Stream Data Types:
cin
to read from the keyboardifstream
objects to read data from filescout
to write to the computer screenofstream
objects to write data into files2. File Access (Mode) Flags:
ios::app
ios::ate
ios::binary
ios::in
ios::out
ios::trunc
ios::out
.Opening a File with the File Stream Object Definition Statement:
An alternative to using the open member function is to use the file stream object definition statement to open the file:
This statement defines a stream object named
dataFile
and uses it to open the file names.txt.The file is opened in both
input
andoutput
modes. This technique eliminates the need to call the open function when your program knows the name and access mode of the file at the time the object is defined.Opening Files with Constructors
ifstream
,ofstream
, andfstream
have initialization constructors that take a filename (as a C-string) and an open mode. C++ 11: The filename can be a string object.open
open
3. Examples
3.1
Write a complete program that reads integers from a file called
numbers.txt
, and displays their sum on the screen.If the content of
numbers.txt
is: 10 20 30 100The program should print: 160
3.2
Write a complete program that uses an
ifstream
object to read integers from a file callednumbers.txt
, and anofstream
object to write into a file calleddoubles.txt
.Your program should have a loop that reads five integers from the input file, then writes the double of each (×2) into the output file. Write each value in a separate line.
3.3
Write a complete program that uses an
ifstream
object to read integers from a file callednumbers.txt
, and anofstream
object to write into a file calledevens.txt
.Your program should have a loop that reads each integer from the input file, then writes it into the output file if it is an even number. Write each even number in a separate line.
The text was updated successfully, but these errors were encountered: