2 - Fundamental File Processing Operations
2 - Fundamental File Processing Operations
Yarmouk University
Department of Computer Information Systems
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!
Application
DBMS
File system
Operating System
Hardware
Physical
Physical Files
Files &
& Logical
Logical Files
Files
Fundamental File Processing Operations 2
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 ;
while (! infile.fail()){
cout << c ;
infile >> c ;
}
infile.close() ;
return 0;
}
Physical
Physical Files
Files &
& Logical
Logical Files
Files ─
─ Revisited
Revisited ## 11
Fundamental File Processing Operations 2
More
More on
on Opening
Opening Files
Files
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
The
The Mode
Mode
Fundamental File Processing Operations 2
Basic
Basic File
File Operations
Operations
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
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
Seeking
Seeking with
with C++
C++ Stream
Stream Classes
Classes
Fundamental File Processing Operations 2
Logical
Logical File
File Names
Names Associated
Associated to
to Std
Std IO
IO Devices
Devices
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) ٢٣
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();
}
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?
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();
}
#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();
}
Binary
Binary file
file
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();
}
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();
}
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
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();
}
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();
}
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();
}