csc1201 Lecture13
csc1201 Lecture13
is permanent;
can be used to
provide input data to a program
or receive output data from a program
or both;
file (fstream)
ifstream - defines new input stream (normally associated with a
file).
ofstream - defines new output stream (normally associated with
a file).
Stream I/O Library Header Files
input_stream.open("numbers.txt“)
Open()
Opening a file associates a file stream variable declared in
the program with a physical file at the source, such as a
disk.
In the case of an input file:
the file must exist before the open statement executes.
If the file does not exist, the open statement fails and the input
stream enters the fail state
An output file does not have to exist before it is opened;
if the output file does not exist, the computer prepares an
empty file for output.
If the designated output file already exists, by default, the old
contents are erased when the file is opened.
Validate the file before trying to access
If ( ! Mystream) If ( ! Mystream.is_open())
{ {
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)
#include <fstream> #include <fstream>
using namespace std;
using namespace std;
int main()
int main()
{
{
//declare output file variable
//declare and automatically ofstream outFile;
open the file
// open an exist file fout.txt
ofstream outFile(“fout.txt");
outFile.open(“fout.txt”);
// Open validation // Open validation
if(! outFile) { if(! outFile.is_open() ) {
Cout << “Cannot open file.\n ”; Cout << “Cannot open file.\n ”;
return 1; return 1;
} }
return 0; return 0;
} }
More Input File-Related Functions
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
#include <iostream>
#include <iostream> #include <fstream>
#include <fstream> #include <string>
int main()
int main() {//Declare and open a text file
{//Declare and open a text file ifstream openFile("data.txt");
ifstream openFile(“data.txt");
string line;
char ch;
while(!openFile.eof())
//do until the end of file
{//fetch line from data.txt and put it in a string
while( ! OpenFile.eof() )
getline(openFile, line);
{
cout << line;
OpenFile.get(ch); // get one character
}
cout << ch; // display the character
openFile.close(); // close the file
}
return 0; }
OpenFile.close(); // close the file
return 0;
}
More Output File-Related Functions
ofstream fsOut;
fsOut.open(const char[] fname)
connects stream fsOut to the external file fname.
fsOut.put(char character)
inserts character character to the output stream fsOut.
fsOut.eof()
tests for the end-of-file condition.
File I/O Example: Writing
First Method (use the constructor) Second Method ( use Open function)
#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
In c++ files we (read from/ write to) them as a stream of
characters
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
}