0% found this document useful (0 votes)
3 views

48 Lecture 15 - (File Handling)

The document discusses file handling in C++, highlighting similarities with console I/O, file writing and reading examples, and the differences between C++ strings and C-style strings. It explains the concept of files, their types (text and binary), and the importance of file structure and format for reading and writing operations. Additionally, it lists well-known file formats and their associated software, along with common file handling functions and modes in C++.

Uploaded by

kurovi.akmal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

48 Lecture 15 - (File Handling)

The document discusses file handling in C++, highlighting similarities with console I/O, file writing and reading examples, and the differences between C++ strings and C-style strings. It explains the concept of files, their types (text and binary), and the importance of file structure and format for reading and writing operations. Additionally, it lists well-known file formats and their associated software, along with common file handling functions and modes in C++.

Uploaded by

kurovi.akmal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

File handling

Similarities with console I/O


• Same << and >> stream insertion and extraction
operations are used to manipulate data in text file
• In case of console I/O, the identifiers cout and cin
are built in C++ and hence readily available
• For files we have to
• Include header fstream
• declare a file stream variable
• Open a file, before reading/writing data
• Instructions for read or write data, as required
• After reading/writing data, we have to close the file.
File writing example
#include <iostream> ofstream
#include <cstdlib> File opened
#include <fstream>
explicitly in
using namespace std; next statement

int main()
{
string numfile = "numbers.txt";
ofstream ofile;
ofile.open(numfile.c_str());
//if (!ofile)
int j=0
while(j<10)
{
ofile << j << " " << rand()%850+150 << endl;
j = j + 1;
}

ofile.close();

return 0;
}
File reading example
#include <iostream>
#include <fstream>

using namespace std; ifstream


Opening file
int main() implicitly
{
string numfile = "nums.txt";
ifstream ifile(numfile.c_str());
//if (!ifile)
int a[10];
for(int j=0; j<10; j++)
{
ifile >> a[j];
cout << a[j] << ",";
}
cout << '\b' << " " << endl;
ifile.close();

return 0;
}
Reading/writing in different files
#include <iostream> char c;
#include <fstream> ifile >> c;
//c = ifile.get();
using namespace std; while(!ifile.eof())
{
int main() if(c >= 'a' && c <= 'z')
{ {
string infile; c = c-'a'+'A';
cout << "Enter input file name "; }
cin >> infile; ofile << c;
ifile >> c;
string outfile; //c = ifile.get();
cout << "Enter output file name "; }
cin >> outfile;
ifile.close();
ifstream ifile(infile.c_str()); ofile.close();
//if (!ifile)
ofstream ofile(outfile.c_str()); return 0;
//if (!ofile) }

Code to make all small letter from input file to


capital letter in output file, leaving all other
character as they were
C++ string vs. C style string
• In C++, string is a data type used to store strings. Full functionality may
be used by including string header (#include <string>), though limited
functionality is available in iostream header.
• string name = “Abdullah”;
• In C, not C++, string are nothing but array of char datatype. In char array,
stored string starts from 0 index of array and finished at a location
where NULL character \0 is placed (automatically by following
statement).
• char name[] = “Abdullah”;
• Some of C++ functions requires string’s data in the form of C style
strings, e.g. open function for file variables.
• From a string variable named str, we can have equivalent C style sting as
str.c_str(). And a C style string named address can be converted into
string type using type casting function string as string(address)
What is a file?
• A file is a sequence of bytes stored on storage media (disk, cd, flash
drive, etc.). Every file in one folder has a unique name.
• Files are two types:
• Text files: Contains formatted data, which is human readable, consists of alphabets,
digits, punctuations, special characters, spaces, and new lines characters. Using any
of commonly available text editors, one can view/edit data stored in text files.
• Binary files: Contains application/software specific data. Only the specific software
can use/display/modify data in binary files. E.g. Pictures stored on computer requires
image viewing software to view them and image editing software is required to edit
them. When these files are open in text editors, funny character are displayed and in
general, humans are unable to read/understand them.
File structure/format
• File are used to store data, and reading/writing files Header
generally requires no user interaction during the Data
execution of the program.
• Data in the files generally has some
structure/format. Which may be described in file
itself in header area of file or in some other file or
implicitly known to the programmer. BMP File Format
The 54 byte BMP header
• Reading and writing data in files should strictly
follow the structure/format of the file, otherwise
later on file reading can results in errors.
• Some of the file related processing may be done
without knowing the structure, e.g. counting lines in
the file, counting characters in the file and capitalize
small alphabets, etc.
Well-known file formats
• There are variety of files in a computer system. Some of
them, along with name of software that may manipulate
them are mentioned below.
• PDF: not possible to open in image viewer or text editor, only software
recognize PDF file structure/format may view/edit it.
• JPG: not possible to open in text editors, only software recognize JPG file
structure/format may view/edit it. There are dozens of such software.
• DOC or DOCX: generally Microsoft word is used to edit/view/print it.
• Many examples, XLSX, PPTX, MP3, MP4, etc.
• EXE: these files are created by compilers and operating system loads them
to memory and executes them.
• Software capable recognizing structure of a file type can
manipulate it.
Same file, different views
A file (without any change in stored bytes) may be
understandable by different software and they treated data
differently. E.g., A PPM (Portable Pixel Map) have following
views using a Notepad++ and IrfanView software.

https://en.wikipedia.org/wiki/Netpbm_format
File handling functions
• Every iostream functionality may be used
with files, some commonly used functions
for data manipulation are:
• put() and get(), getline(), ignore(), peek(), etc.
• io manipulators like endl, setw(), etc.
• read() and write() for binary data.
• Besides data manipulation, some of the
functions are available for testing the state of
the stream, e.g.,
• good(), is_open(), eof(), clear(), etc.
More file handling functions
File open modes
If more than one
mode/flag is required,
character | has to be in
between
e.g. ios::in | ios::out | ios::ate

Random Access

To be continued . . .

You might also like