0% found this document useful (0 votes)
17 views42 pages

Chapter 4 - Files

The document discusses working with data files in C++, including reading and writing files using file streams, opening and closing files, and error checking when opening files. It covers reading data from files using ifstream objects and the extraction operator, as well as writing data to files using ofstream objects and the insertion operator. Various file open modes like append, binary, and output are also described.

Uploaded by

Moncef Dahaji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views42 pages

Chapter 4 - Files

The document discusses working with data files in C++, including reading and writing files using file streams, opening and closing files, and error checking when opening files. It covers reading data from files using ifstream objects and the extraction operator, as well as writing data to files using ofstream objects and the insertion operator. Various file open modes like append, binary, and output are also described.

Uploaded by

Moncef Dahaji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Chapter 4

Working with Data Files


Objectives:
• Introduction to files and file streams
• Reading data files
• Error checking
• Writing into a data file
• File format
• Binary Files
• Numerical technique: linear modeling

Reading Material:
Etter and Ingber book, Chapter 5 (Working with Data Files)

Fall 2022, Mohamad Eid ENGR–UH 1000


Computing Concepts: Files and Streams
 Files are commonly used for the permanent storage of large quantities of data.
 Files are saved on a secondary storage device such as a hard drive; these disks
can hold files of different types:
– Text files: assignment1_report.docx
– Program files: assignment1.cpp
– Executable files: assignment1.exe
– Data files: ECG.dat or users.txt
 Files are usually partitioned and written over many tracks and sectors of a
disk.
Disk

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

Output File Steam Input File Stream


Program Program
Variables
10 25 40 Variables 10 25 40
X 10 X 10

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

 The Standard C++ library defines stream classes to support file


input and output.
– ofstream: The file is opened for output only. Information may be
written to the file, but not read from the file. If the file does not exist, it
is created. If the file already exists, it’s contents are deleted (the file is
truncated).
– ifstream: The file is opened for input only. Information may be read
from the file, but not written to it. The file’s contents will be read from
its beginning. If the file does not exist, the open function fails.
The ifstream Class
 The ifstream (input file stream)
class is derived from istream
(input stream class).
– Thus ifstream inherits all operators
(i.e. >>) and member functions (i.e.
eof()) defined in istream.
– The ifstream class defines
additional methods specific to
working with files.
Input File Streams
 File access requires the inclusion of “fstream.h” library
#include <fstream>

 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++?

The file is closed implicitly when a destructor for the


corresponding object is called

OR

by using member function close:

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++?

The file is closed implicitly when a destructor for the


corresponding object is called

OR

by using member function close:

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::binary – Binary mode. When a file is opened in binary mode, information


is written to or read from it in pure binary format. The default mode is text.

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;

ofstream outClientFile( "clients.rft", ios::out );

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? ";

while ( cin >> account >> name >> balance ) {


outClientFile << account << ' ' << name
<< ' ' << balance << '\n';
cout << "? ";
}
outClientFile.close();
return 0; // ofstream destructor closes file
}
#include <iostream>
#include <fstream>
#include <iomanip>
Reading and printing a
using namespace std;

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";

while ( inClientFile >> account >> name >> balance )


{
cout<<left<<setw(10)<<account<<setw(13)<<name<<setw(7)<<balance<<endl;
}
return 0; // ifstream destructor closes the file
}
File Formats
 To correctly read an input file, some knowledge of the
contents is required:
– Name of the file.
– Order and data types of the values stored in the file.

 Three common structures:


– The first line in file contains the number of lines/records in the file.
– Trailer signal/sentinel value is provided to indicate the last
line/record in the file.
– End-of-file is used to indicate last line/record in the file.
Specified Number of Lines
sensor1.dat //open input file
10 ifstream sensor1(“sensor1.dat”);
if(sensor1.fail()) exit(1);
0.0 132.5
0.1 147.2 //read the number of data entries
0.2 148.3 int numEntries;
0.3 157.3 sensor1 >> numEntries;
//read every row
0.4 163.2
double t, y;
0.5 158.2 for (int i = 0; i < numEntries; i++) {
0.6 169.3 sensor1 >> t >> y;
0.7 148.2 //do something with the data
0.8 137.6 }
sensor1.close(); //close the file
Trailer/Sentinel Value
sensor2.dat //open input file
0.0 132.5 ifstream sensor2(“sensor2.dat”);
0.1 147.2 if(sensor2.fail()) exit(1);
0.2 148.3 //read every row
0.3 157.3 double t, y;
0.4 163.2
do {
0.5 158.2
sensor2 >> t >> y;
0.6 169.3
//do something with the data
} while (t != -99);
0.7 148.2
0.8 137.6
-99 -99 sensor2.close(); //close the file
EOF-based File Input
sensor3.dat //open input file
0.0 132.5 ifstream sensor3(“sensor3.dat”);
0.1 147.2 if(sensor3.fail()) exit(1);
0.2 148.3 //read every row
0.3 157.3 double t, y;
0.4 163.2
sensor3 >> t >> y;
0.5 158.2
while (!sensor3.eof()) {
//do something with the data
0.6 169.3
sensor3 >> t >> y;
0.7 148.2
}
0.8 137.6
sensor3.close(); //close the file
Whitespace
 Whitespace is defined as a blank, a tab, a newline, a form feed, or a carriage
return character.

 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?

char ch; char ch; Input Stream:


while (!infile.eof()) { while (!infile.eof()) { 10 20
infile >> ch; infile.get(ch); 30
cout << ch; } cout << ch; }

Can’t read spaces! Can read spaces!


Example
Develop a program to copy the contents of a file named « MyFile.txt » into
another file named « Copy.txt »
Binary Files
• Data can represent anything from:
• simple text
• images
• weather maps
• social media sharing

• 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.

 The disadvantages of a binary file are listed below.


– In general they cannot be written or read using a text editor.
– They are severely limited in portability since they must be read using variables that have the same
binary representation as the stored data. !
 Limited portability of data between programming languages and machines.
Opening Binary Files
ifstream binaryInstream;

binaryInstream.open("YellowBox.jpg",ios::binary);

if(binaryInstream.fail())
exit(1);

ofstream binaryOutstream("modifiedImage.bin”, ios::binary);

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);

data is the starting address of the source of the data


// reading an entire binary file
Example #include <iostream>
#include <fstream>

using namespace std;

int main ()
{
streampos size;
char * memblock;

ifstream file ("example.bin", ios::in|ios::binary|ios::ate);

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
‫متشکرم‬

Thank You! Pақмет

You might also like