Files
Files
Files
net/publication/348960289
Files in C++
CITATIONS READS
0 7,245
1 author:
Tarfa Hamed
University of Mosul
38 PUBLICATIONS 263 CITATIONS
SEE PROFILE
All content following this page was uploaded by Tarfa Hamed on 02 February 2021.
ofstream
1.
This data type represents the output file stream and is used to create files
and to write information to files.
ifstream
2.
This data type represents the input file stream and is used to read
information from files.
fstream
3. This data type represents the file stream generally, and has the
capabilities of both ofstream and ifstream which means it can create
files, write information to files, and read information from files.
1
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
Opening a File
• A file must be opened before you can read from it or write to it.
Either ofstream or fstream object may be used to open a file for
writing.
• And ifstream object is used to open a file for reading purpose
only.
• Following is the standard syntax for open() function, which is a
member of fstream, ifstream, and ofstream objects.
• Here, the first argument specifies the name and location of the
file to be opened and the second argument of the open() member
function defines the mode in which the file should be opened.
1. ios::app
Append mode. All output to that file to be appended to the end.
2. ios::ate
Open a file for output and move the read/write control to the end of
the file.
3. ios::in
Open a file for reading.
4. ios::out
Open a file for writing.
5. ios::trunc
If the file already exists, its contents will be removed before opening
the file.
2
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
• Similar way, you can open a file for reading and writing purpose
as follows:
fstream afile;
afile.open("file.dat", ios::out | ios::in );
Closing a File
• When a C++ program terminates it automatically flushes all the
streams, release all the allocated memory and close all the opened
files.
• But it is always a good practice that a programmer should close
all the opened files before program termination.
• Following is the standard syntax for close() function, which is a
member of fstream, ifstream, and ofstream objects.
File_name.close();
Writing to a File
• You can write information to a file from your program using the
stream insertion operator (<<) just as you use that operator to
output information to the screen.
• The only difference is that you use an ofstream or fstream object
instead of the cout object.
3
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
myfile.open("example.txt");
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
4
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
return 0;
}
int main () {
string data;
// open a file in write mode.
ofstream outfile;
outfile.open("afile.dat",ios::out );
int age;
cout << "Enter your age: ";
cin >> age;
5
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
return 0;
}
HW1: Write a CPP program that reads items names and items prices
of 50 items from KB and stores them in a file (prices.txt) line-by-line.
After that, the program reads the previous file and writes the items that
are priced over $100 in a separate file (prices_100.txt).
6
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq