Chapter 4 - Files
Chapter 4 - Files
Reading Material:
Etter and Ingber book, Chapter 5 (Working with Data Files)
Sector
Track
Computing Concepts: Files and Streams
The operating system
– Maintains a directory tree that lists all files stored on all disks;
– The directory associates each file to its physical location(s) on the disk; and
– Maintains the File Control Block (FCB) that allows a program to access a file stored
on a disk.
A file can be written or read by a C++ program.
– Standard C++ functions are available and can be used to create and manipulate files.
A file is viewed in C++ as a sequence of bytes.
– When a file is opened, a stream is associated with the file.
– A stream can be viewed as a communication channel between the program and the
opened file.
– The stream is a structure manipulated by the standard functions.
Programming Model for Input and Output
I/O Streams
buffer
aStream
cin buffer cout buffer bStream buffer
Hard disk
Screen
a.txt
Keyboard
Operating System b.txt
The Process of Using a File
Using a file in a program is a simple three-step process
– The file must be opened. If the file does not yet exist, opening
it means creating it.
– Information is then saved to the file, read from the file, or both.
– When the program is finished using the file, the file must be
closed.
Read/Write from/into a File
Y 25 Y 25
Z 40 Z 40
Data is copied from variables into the file Data is copied from the file into variables
File Streams
Before data can be read from a file, the file must be opened:
ifstream inFile;
inFile.open(“MyFile.txt”);
OR
ifstream sensor1(“sensor1.dat”);
If the data file specified in the open() statement can not be found, the input file stream will
be placed in a fail state.
Testing for Open Errors
All statements that reference the failed input stream are ignored.
– NO error message will be generated and the program will continue to execute.
– Use the fail() function to check the state of the input stream.
ifstream sensor1;
sensor1.open("sensor1.txt");
if ( sensor1.fail() ) //open failed
{
cerr << "File sensor1.txt could not be opened";
exit(1); //end execution of the program
}
sensor1 >> x; // read data from file and save in x
Read Information from a File
The input operator (>>) may be used to read information from a file.
ifstream sensor1;
sensor1.open("sensor1.txt");
if ( sensor1.fail() ) //open failed
{
cerr << "File sensor1.txt could not be opened";
exit(1); //end execution of the program
}
sensor1 >> x; // read data from file and save in x
How to close a file in C++?
OR
sensor1.close();
Example
Write a C++ program to read the temperature values from a file named “temps.txt”
and display them on the output screen. Assume first line has the number of
temperature values in the file. Also print the average temperature to the user.
temps.txt
5
27
33
41
17
21
The ofstream Class
The ofstream (output file stream)
class is derived from ostream
(output stream class).
– Thus ofstream inherits the output
operator and member functions
defined in ostream.
– The ofstream class defines
additional methods specific to
working with files.
Output File Streams
File access requires the inclusion of “fstream.h” library
#include <fstream>
Before data can be written into a file, the file must be opened:
ofstream outFile;
outFile.open(“MyFile.txt”);
OR
ofstream outFile(“MyFile.txt”);
Testing for Open Errors
All statements that reference the failed input stream are
ignored.
– NO error message will be generated and the program will continue to execute.
– Use the fail() method to check the state of the input stream.
ofstream outFile;
outFile.open(”myFile.txt");
if (outFile.fail() ) //open failed
{
cerr << "File sensor1.txt could not be opened";
exit(1); //end execution of the program
}
outFile << “I love programming”; // Write message in file
Write Information into a File
The output operator (<<) may be used to read information from
a file.
ofstream outFile;
outFile.open(”myFile.txt");
if (outFile.fail() ) //open failed
{
cerr << "File sensor1.txt could not be opened";
exit(1); //end execution of the program
}
outFile << “I love programming”; // Write message in file
How to close a file in C++?
OR
outFile.close();
Example
Write a program that creates a file named ‘MyFile.txt’ and stores the following
message: «Hi, this is my file!»
File Open Modes
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 not exist.
ios::ate – If the file already exists, the program goes directly to the end of it.
Output may be written anywhere in the file.
ios::in - Input mode. Information will be read from the file. If the file does not
exist, it will not be created and the open function will fail.
File Open Modes
ios::out – Output mode. Information will be written to the file. By default, the
file’s contents will be deleted if it already exists. A file is created if it does not
exist
ios::trunc -(truncate) If the file already exists, its contents will be deleted
(truncated). This is the default mode used by the ios::out.
ios::nocreate - If the file does not exists, this flag will cause the open function
to fail. The file will not be created.
ios::noreplace - If the file exists, this flag will cause the open function to fail.
The existing file will not be opened.
#include <iostream>
#include <fstream>
using namespace std;
Writing into sequential file
?
int main()
{
int account;
string name;
float balance;
if ( outClientFile.fail() ) {
cerr << "File could not be opened" << endl;
exit( 1 );
}
cout << "Enter the account number, name, and balance.\n"
<< "Enter end-of-file to end input.\n? ";
int main()
sequential file
{
// ifstream constructor opens the file
ifstream inClientFile( "clients.rft", ios::in );
if (!inClientFile) {
cerr << "File could not be opened\n";
exit( 1 );
}
int account;
string name;
double balance;
cout <<left<<setw(10)<<"Account"<<setw(13)<<"Name"<<"Balance\n";
The C++ input operator >> expects whitespace as the delimiter when
reading data from an input stream.
– C++ ignores and passes over the whitespace to read the expected data.
– Reading whitespaces from file?
• Big data is a term that refers to data sets that are so large and so
complex that they are not easily defined.
• Big data can arrive from a variety of devices and it arrives in very
large volumes and at very high rates of speed.
• For large data sets, binary files are preferred.
Binary Files
A binary file is simply a sequence of bytes that have been copied directly from memory
(RAM) into a file; a binary file is in effect a memory dump.
The advantages of a binary file are listed below.
– Binary files are usually smaller compared to an ASCII file containing the same information.
– Input and output operations between a binary file and a program are usually faster than input and
output operations with an ASCII file since data conversions are not required.
E.g.: A real number, read in as a sequence of bytes in an ASCII file (where each byte represents a digit or the
decimal point), must be converted to a double before it is stored in a variable of type double. Such a
conversion is not necessary when reading a binary file.
binaryInstream.open("YellowBox.jpg",ios::binary);
if(binaryInstream.fail())
exit(1);
binaryOutstream.close();
Reading and Writing Binary Files
The istream class defines a member function with the following
prototype for reading unformatted data from a binary file:
istream& read(char* data, streamsize n);
data is the starting address of a range of memory
The ostream class defines a member function with the following prototype for
writing unformatted data to a binary file:
ostream& write(const char* data, streamsize n);
int main ()
{
streampos size;
char * memblock;
if (file.is_open())
{
size = file.tellg(); // returns the size of the file
memblock = new char [size];
file.seekg (0, ios::beg); // make sure the file pointer is at the beginning
file.read (memblock, size);
file.close();
cout << "the entire file content is in memory";
delete[] memblock;
}
else
cout << "Unable to open file";
return 0;
}
Linear Modeling
Linear modeling is the name given to the process that determines the linear
equation (y = mx + b) that is the best fit to a set of data points.
– Linear regression does this by minimizing the squared distance between the line
and the data points.
Ozone Measurements
Ozone Measurements
Ozone Measurements
متشکرم