1. Chapter: 12
Stream and Files
Lecture: 49
Date: 13.11.2012
2. Objectives
Overview of stream classes
Showing how to perform file-related activities using streams:
How to read and write data to files in a variety of ways
How to handle files or I/O errors
How files and OOP are related
3. Streams
Stream (flow of data)
A transfer of information in the form of a sequence of bytes
In C++, a stream is represented by an object of a
particular class; e.g., cin and cout are objects of
iostream class.
I/O Operations:
Input: A stream that flows from an input device ( i.e.: keyboard,
disk drive, network connection) to main memory
Output: A stream that flows from main memory to an output
device ( i.e.: screen, printer, disk drive, network connection)
4. keyboard
standard
input stream
CPU
standard
output MEM
monitor stream
terminal
console
HDD
What information travels
across?
Streams
5. keyboard
standard
input stream
CPU
standard
output MEM
monitor stream
terminal file
console input
stream
LOAD HDD
What information READ
travels across? file
files output
Streams stream
SAVE
WRITE
7. What is a File?
File: A file is a collection on information, usually
stored on a computer’s disk. Information can be
saved to files and then later reused.
File Names: All files are assigned a name that is
used for identification purposes by the operating
system and the user.
8. File Names and Extensions
File Name and File Contents
Extension
M Y P R O G .B A S BASIC program
M E N U .B A T DOS Batch File
IN S T A L L .D O C Documentation File
C R U N C H .E X E Executable File
B O B .H T M L HTML (Hypertext Markup Language) File
3 D M O D E L .J A V A Java program or applet
IN V E N T .O B J Object File
P R O G 1 .P R J Borland C++ Project File
A N S I.S Y S System Device Driver
R E A D M E .T X T Text File
11. Disk File I/O with Streams
Most programs need to save data to disk files and read it
back in.
Working with disk files requires an other set of classes:
ifstream for (file) input
fstream for both (file) input and output
ofstream for (file) output
Objects of these classes can be associated with disk
files, and their member functions can be used to
read and write to files.
12. The Process of Using a File
Using a file in a program is a simple three-step process
1) The file must be opened. If the file does not yet exits,
opening it means creating it.
2) Information is then saved to the file, read from the file, or
both.
3) When the program is finished using the file, the file must
be closed.
13. #include <iostream>
#include <fstream>
#include <conio.h> Writing to File
using namespace std;
int main()
{ char ch = ‘x’;
int j = 77;
double d = 6.02;
string str1 = “Book”; //strings without embedded spaces
string str2 = “Pen”;
ofstream outfile(“fdata.txt”); //create ofstream object
outfile << ch //insert (write) data
<< j
<< ‘ ‘ //needs space between numbers
<< d
<< str1
<< ‘ ‘ //needs spaces between strings
<< str2;
cout << “File writtenn”;
getch(); return 0; }
14. #include <iostream>
#include <fstream>
#include <conio.h> Reading from File
using namespace std;
int main()
{ char ch;
int j;
double d;
string str1;
string str2;
ifstream infile("fdata.txt");//create ifstream object
//extract (read) data from it
infile >> ch >> j >> d >> str1 >> str2;
cout << ch << endl //display the data
<< j << endl
<< d << endl
<< str1 << endl
<< str2 << endl;
getch(); return 0; }
15. Writing to File (easy way)
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
int main () {
ofstream myfile ("example.txt");
if (myfile.is_open())
{
myfile << "This is a line.n";
myfile << "This is another line.n";
myfile.close();
}
else cout << "Unable to open file";
getch();
return 0; }
16. Reading from File (easy way)
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile, line); //extract characters into object myfile until line
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
getch(); return 0; }
Notas do Editor
Student Book
Used tootsie roll pieces as data bytes and a large tootsie roll as a line of data on a cardboard card and drawing on the chalkboard.
Used tootsie roll pieces as data bytes and a large tootsie roll as a line of data on a cardboard card and drawing on the chalkboard.