0% found this document useful (0 votes)
2K views

2 - Fundamental File Processing Operations

This document discusses fundamental file processing operations such as opening, closing, reading from and writing to files in C and C++. It explains that a physical file exists on storage, while a logical file is what a program uses through file operations. The operating system links logical files to physical files and handles errors. Basic file operations like opening a file assign it a descriptor and initialize it for the program. Reading and writing characters from/to a file are demonstrated in sample C++ code. Physical and logical files are revisited with the OS responsible for associating them.

Uploaded by

Nagaraj Gadagin
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

2 - Fundamental File Processing Operations

This document discusses fundamental file processing operations such as opening, closing, reading from and writing to files in C and C++. It explains that a physical file exists on storage, while a logical file is what a program uses through file operations. The operating system links logical files to physical files and handles errors. Basic file operations like opening a file assign it a descriptor and initialize it for the program. Reading and writing characters from/to a file are demonstrated in sample C++ code. Physical and logical files are revisited with the OS responsible for associating them.

Uploaded by

Nagaraj Gadagin
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

File Structures

Dr. Qasem Al-Radaideh


[email protected]

Yarmouk University
Department of Computer Information Systems

CIS 256 ( File Structures) ١

2 Fundamental File Processing Operations


Content
Content
Fundamental File Processing Operations 2

►Sample programs for file manipulation


►Physical files and logical files
►Opening and closing files
►Reading from files and writing into files
►How these operations are done in C and C++
►Standard input/output and redirection

CIS 256 (File Structures) ٣

What
What isis aa FILE?
FILE?
Fundamental File Processing Operations 2

I wonder...

A file is...
►A collection of data placed under permanent or
non-volatile storage
►Examples: anything that you can store in a disk,
hard drive, tape, optical media, and any other
medium which doesn’t lose the information when
the power is turned off.
►Notice that this is only an informal definition!

CIS 256 (File Structures) ٤


Where
Where do
do File
File Structures
Structures fit
fit in
in CS?
CS?
Fundamental File Processing Operations 2

Application

DBMS

File system

Operating System

Hardware

CIS 256 (File Structures) ٥

Physical
Physical Files
Files &
& Logical
Logical Files
Files
Fundamental File Processing Operations 2

► Physical file: physically exists on secondary storage;


known by the operating system; appears in its file
directory
► Logical file, what your program actually uses, a ‘pipe’
though which information can be extracted, or sent.
►Operating system: get instruction from program or
command line; link logical file with physical file or device
► Why is the distinction useful? Why not allow our
programs to deal directly with physical files?

CIS 256 (File Structures) ٦


Basic
Basic File
File Operations
Operations
Fundamental File Processing Operations 2

►Opening a file - basically, links a logical file to a physical


file.
– On open, the O/S performs a series operations that end
in the program that is trying to open the file being
assigned a file descriptor.
– Additionally, the O/S will perform particular operations
on the file at the request of the calling program, these
operations are intended to ‘initialize’ the file for use by
the program.
– What happens when the O/S detects an error?

CIS 256 (File Structures) ٧

infile:
infile: Logical
Logical File,
File, “account.txt”:
“account.txt”: Physical
Physical File
File
Fundamental File Processing Operations 2

#include <fstream>
#include <iostream>
using namespace std ;
int main(){
char c;
fstream infile ;
infile.open("account.txt",ios::in) ;
infile.unsetf(ios::skipws) ;
infile >> c ;

CIS 256 (File Structures) ٨


Fundamental File Processing Operations 2

while (! infile.fail()){
cout << c ;
infile >> c ;
}
infile.close() ;
return 0;
}

CIS 256 (File Structures) ٩

Physical
Physical Files
Files &
& Logical
Logical Files
Files ─
─ Revisited
Revisited ## 11
Fundamental File Processing Operations 2

► OS is responsible for associating a logical file in a program to a


physical file in disk or tape. Writing to or reading from a file in a
program is done through the OS.
► Note that from the program point of view, input devices (keyboard)
and output devices (console, printer, etc) are treated as files ─
places where bytes come from or sent to
► There may be thousands of physical files on a disk, but a program
only have a limited number of logical files open at the same time.
► The physical file has a name, for instance “account.txt”
► The logical file has a logical name used for referring to the file
inside the program. The logical name is a variable inside the
program, for instance “infile”

CIS 256 (File Structures) ١٠


Physical
Physical Files
Files &
& Logical
Logical Files
Files ─
─ Revisited
Revisited ## 22
Fundamental File Processing Operations 2

►In C++ PL, the logical name is the name of an object of


the class fstream:
fstream
fstream infile ;
►In both languages, the logical name infile will be
associated to the physical file “account.txt” at the time of
opening the file.

CIS 256 (File Structures) ١١

More
More on
on Opening
Opening Files
Files
Fundamental File Processing Operations 2

►Two options for opening a file:


– Open an existing file
– Create a new file

CIS 256 (File Structures) ١٢


How
How to
to do
do in
in C++
C++
Fundamental File Processing Operations 2

fstream outfile;
outfile.open(“account.txt”, ios::out) ;
►The 1st argument indicates the physical name of the file
►The 2nd argument is an integer indicating the mode
defined in the class ios.
ios

CIS 256 (File Structures) ١٣

The
The Mode
Mode
Fundamental File Processing Operations 2

►ios::in open for reading


►ios::out open for writing
►ios::app seek to the end of file before each write
►ios::trunc always create a new file
►ios::nocreate fail if file does not exist
►ios::binary open in binary mode

CIS 256 (File Structures) ١٤


Basic
Basic File
File Operations
Operations
Fundamental File Processing Operations 2

►Closing a file - cuts the link between physical and logical


files
– Upon closing, the OS takes care of ‘synchronizing’ the
contents of the file, e.g. often a buffer is used, need to
write buffer content to file.
– In general, files are automatically closed when the
program ends.
– So, why do we need to worry about closing files?
– In C++: outfile.close()

CIS 256 (File Structures) ١٥

Basic
Basic File
File Operations
Operations
Fundamental File Processing Operations 2

►Reading and Writing – basic I/O operations.


– Usually require three parameters: a logical file, an
address, and the amount of data that is to be read or
written.
– What is the use of the address parameter?

CIS 256 (File Structures) ١٦


Reading
Reading in
in C++
C++
Fundamental File Processing Operations 2

char c ; // a character
char a[100] ; // an array with 100 characters
fstream infile ;
infile.open(“myfile.txt”, ios::in) ;
infile >> c; // reads one character
infile.read(&c,1) ;
infile.read(a,10); // reads 10 bytes
►Note that thanks to operator overloading in C++,
operator >> gets the same info at a higher level

CIS 256 (File Structures) ١٧

Writing
Writing in
in C++
C++
Fundamental File Processing Operations 2

char c ; // a character
char a[100] ; // an array with 100 characters
fstream outfile ;
outfile.open(“myfile.txt”, ios::out) ;
outfile << c; // writes one character
outfile.write(&c,1) ;
outfile.write(a,10); // writes 10 bytes

CIS 256 (File Structures) ١٨


Additional
Additional File
File Operations
Operations
Fundamental File Processing Operations 2

►Seeking: source file, offset.


►Detecting the end of a file
►Detecting I/O error

CIS 256 (File Structures) ١٩

Seeking
Seeking with
with C++
C++ Stream
Stream Classes
Classes
Fundamental File Processing Operations 2

A fstream has 2 file pointers: get pointer & put pointer


(for input) (for output)
file1.seekg ( byte_offset, origin); //moves get pointer
file1.seekp ( byte_offset, origin); //moves put pointer

origin can be ios::beg (beginning of file)


ios::cur (current position)
ios::end (end of file)

file1.seekg ( 373, ios::beg); // moves get pointer 373 bytes from


// the beginning of file
CIS 256 (File Structures) ٢٠
Detecting
Detecting End
End of
of File
File
Fundamental File Processing Operations 2

►In C++: Check whether infile.fail() returns true


infile >> c ;
if (infile.fail()) // true if file has ended
►Alternatively, use the function infile.eof()
►Also note that fail() indicates that an operation is
unsuccessful, so it is more general than just checking for
end of file

CIS 256 (File Structures) ٢١

Logical
Logical File
File Names
Names Associated
Associated to
to Std
Std IO
IO Devices
Devices
Fundamental File Processing Operations 2

Purpose Default Logical Name


Meaning in C in C++
Standard Output Console/Screen stdout cout
Standard Input Keyboard stdin cin
Standard Error Console/Screen stderr cerr

►These streams do not need to be open or closed in the


program

CIS 256 (File Structures) ٢٢


Ex: Create
Createaatext
textfile
file
Fundamental File Processing Operations 2

Write a C++ program that create a text file for N name of instructors :

#include<iostream.h>
#include<fstream.h>
void main()
{ int n;
char name[50];
ofstream x ("name.txt“);
if (!x) { cout << “The file not found"; }
cin >> n;
cout << "enter the number of instructor“ << endl;
for ( int i=0; I <=n ; i++)
{ cout << “Enter the name of instructor“ << endl;
cin >> name;
cout << endl;
x << name;
}
x.close(); }
CIS 256 (File Structures) ٢٣

Q2 : Write program to display the content of the file?


Fundamental File Processing Operations 2

include<iostream.h>
include<fstream.h>
void main()
{
char name[50];
ifstream x ("name.txt");
while(1)
{
x >> name;
if (x.fail()) break;
cout << “Name =“ << name << endl;
}
x.close();
}

CIS 256 (File Structures) ٢٤


Question
Fundamental File Processing Operations 2

1-Write program to display the content of file such as

A-Display the name of student that begin with letter “A” or “C”?
B-Display the name of student that begin with small letter ?
C-Display the number of names in file?

CIS 256 (File Structures) ٢٥

Solution
Solution
Fundamental File Processing Operations 2

void main()
{ int z;
char name[50];
ifstream x("name.txt");
while(1)
{ x.getline (name, 30);
if (x.fail()) break;
if (name[0] == “A” || name[0] == “C”)
cout << “Name =“ << name << endl;
if (name[0] >= “a” && name[0] <= “z”)
cout << “Name =“ << name << endl;
z++;
}
cout<<z;
x.close();
}

CIS 256 (File Structures) ٢٦


Text
Text file
file by
by using
using EOF
EOF
Fundamental File Processing Operations 2

#include<iostream.h>
#include<fstream.h>
void main()
{
char n[20];
ifstream Z(“student.txt”);
Z.getline(n,15);
While(!Z.eof())
{
cout<<“n =“<<n<<endl;
Z.getline(n,15);
}
Z.close();
}

CIS 256 (File Structures) ٢٧

Binary
Binary file
file
Fundamental File Processing Operations 2

- The binary file is two type


A-File of integer or Char
B-File of record
- Extension of binary file is name of file.date
The binary file contain two statement one for read and other for write
- statement of write is
logical name.write((unsigendchar*)&object,sizeof(object));
-statement for read
logical name.read((unsigendchar*)&object,sizeof(object));

CIS 256 (File Structures) ٢٨


A-
File
File of
of integer
integer
Fundamental File Processing Operations 2

vide main()
{
int Mark;
ofstream Z(“mark.dat”)
if(!Z)
{ cout<<“file cannot open”; break; }
wile(1)
{cin>>Mark;
if(Mark==999) break;
Z.write((unsigned char*)&Mark,sizeof(Mark));
}
Z.close();
}

CIS 256 (File Structures) ٢٩

B-
File
File of
of record
record
Fundamental File Processing Operations 2

struct student
{char name[10], int Age, int Mark; };
void main()
{student S;
ofstream X(“student.dat”);
cin>>S.Mark;
while(S.Mark !=-1)
{
cin>>S.name>>S.Age;
X.write ((unsigned char*)&S,sizeof(S));
if(X.fail()) break;
}
cin>>S.Mark;
X.close();
}

CIS 256 (File Structures) ٣٠


Q:
Write
Write program
program to
to display
display the
the content
content of
of the
the file
file
Fundamental File Processing Operations 2

struct student
{char name[10], int Age, int Mark; };
void main()
{student S;
ifstream X(“student.dat”);
X.read ((unsigned char*)&S,sizeof(S));
if(X.fail()) break;
cout<<“NAME<<S.name<<endl;
cout<<“MARK”<<S.Mark<<endl;
cout<<“AGE”<<S.Age<<endl;
}
cin>>S.Mark;
X.close();
}
CIS 256 (File Structures) ٣١

Question
Question
Fundamental File Processing Operations 2

1- Find the sum of mark and average.


2- find the maximum of mark.
3- find the name of student who take the maximum mark.
4- count the number of student who take the mark is 99

CIS 256 (File Structures) ٣٢


solution
solution
1+2
Fundamental File Processing Operations 2

struct student
{char name[10], int Age, int Mark; };
void main()
{Student S;
Int sum=0, avg, d;
Int max=0;
ifstream X(“student.dat”);
X.read ((unsigned char*)&S,sizeof(S));
if(X.fail()) break;
Sum = sum + S.Mark;
if(S.Mark > Max)
Max = S.Mark
d++;
Cout<<“MAX =”<<Max;
}
cout<<“avg = sum/d”<<sum/d<<endl;
X.close();
}

CIS 256 (File Structures) ٣٣

Solution
Solution
3
Fundamental File Processing Operations 2

struct student
{char name[10], int Age, int Mark; };
void main()
{student S;
Int max=0 , int d;
ifstream X(“student.dat”);
X.read ((unsigned char*)&S,sizeof(S));
if(X.fail()) break;
if(S.Mark > Max)
Max = S.Mark
S.name = Max;
d++;
Cout<<“NAME =”<<S.name;
}
X.close();
}

CIS 256 (File Structures) ٣٤


Solution
Solution 44
Fundamental File Processing Operations 2

struct student
{char name[10], int Age, int Mark; };
void main()
{student S;
int d;
ifstream X(“student.dat”);
X.read ((unsigned char*)&S,sizeof(S));
if(X.fail()) break;
if(S.Mark == 99)
d++;
Cout<<“NAME =”<<S.name;
}
Cout<<“d=“<<d<<endl;
X.close();
}

CIS 256 (File Structures) ٣٥

You might also like