Chapter 11 - Files
Chapter 11 - Files
INTRODUCTION TO PROGRAMMING
Chapter 11
Files
Learning Objectives
▪ is permanent;
▪ can be used to
▪ provide input data to a program
▪ or receive output data from a program
▪ or both;
5. Use the file stream variables with >>, <<, or other input/output
functions.
▪ file (fstream)
▪ ifstream - defines new input stream (normally associated
with a file).
▪ ofstream - defines new output stream (normally
associated with a file).
▪ fstream - it provides member functions for opening,
reading, writing, and closing files.
Using Input/Output Files
▪ ifstream (input file stream) is a class derived from the istream
base class, and it is used for reading data from files. The ifstream
class provides functionalities to open, read, and close files.
input_stream.open("numbers.txt“)
Open()
If ( ! Mystream.is_open()) {
If ( ! Mystream) Cout << “File is not open.\n ”;
{ }
Cout << “Cannot open file.\n ”;
}
File I/O Example: Open the file with validation
First Method (use the constructor) Second Method ( use Open function)
▪ ifstream fsin;
▪ fsin.open(const char[] fname)
▪ connects stream fsin to the external file fname.
▪ fsin.get(char character)
▪ extracts next character from the input stream fsin and
places it in the character variable character.
▪ fsin.eof()
▪ tests for the end-of-file condition.
File I/O Example: Reading
Read char by char Read a line
return 0; outFile.close();
}
return 0;
}
File Open Mode
Name Description
ios::in Open file to read
ios::out Open file to write
ios::app All the data you write, is put at the end of the file. It calls
ios::out
ios::ate All the data you write, is put at the end of the file. It does not
call ios::out
ios::trunc Deletes all previous content in the file. (empties the file)
ios::nocreate If the file does not exists, opening it with the open() function
gets impossible.
ios::noreplace If the file exists, trying to open it with the open() function,
returns an error.
ios::binary Opens the file in binary mode.
File Open Mode
ios::in
▪ Input mode: Used for reading data from a file. The file must exist;
otherwise, the opening will fail.
ios::out
▪ Output mode: Used for writing data to a file. If the file already exists,
its contents will be truncated. If the file doesn't exist, a new file will
be created.
ios::ate
▪ At end mode: Used to position the file pointer at the end of the file
immediately after opening it. This is useful when you want to both
read and write data to the file without overwriting its contents.
File Open Mode
#include <fstream>
int main(void)
{
ofstream outFile("file1.txt", ios::out);
outFile << "That's new!\n";
outFile.close();
Return 0;
}
If you want to set more than one open mode, just use the OR operator- |.
This way:
ios::ate | ios::binary
Summary of Input File-Related Functions
#include <fstream>
ifstream fsIn;
▪ fsIn.open(const char[] fname)
▪ connects stream fsIn to the external file fname.
▪ fsIn.get(char& c)
▪ extracts next character from the input stream fsIn and places it in
the character variable c.
▪ fsIn.eof()
▪ tests for the end-of-file condition.
▪ fsIn.close()
▪ disconnects the stream and associated file.
▪ fsIn >> c; //Behaves just like cin
Summary of Output File-Related Functions
#include <fstream>
ofstream fsOut;
▪ fsOut.open(const char[] fname)
▪ connects stream fsOut to the external file fname.
▪ fsOut.put(char c)
▪ inserts character c to the output stream fsOut.
▪ fsOut.eof()
▪ tests for the end-of-file condition.
▪ fsOut.close()
▪ disconnects the stream and associated file.
▪ fsOut << c; //Behaves just like cout
File format
if (!outFile.is_open())
{ cout << " problem with opening the file ";}
else
{outFile <<200 <<endl ;
cout << "done writing" <<endl;}
outFile.close();
}
Example Reading from file
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
void main()
{//Declare and open a text file
ifstream INFile("number.txt");
string line;
int total=0;
while(! INFile.eof())
{
getline(INFile, line);
//converting line string to int
stringstream(line) >> total;
cout << line <<endl;
cout <<total +1<<endl;}
INFile.close(); // close the file
}
*** The End ***