2 - Fundamental File Processing Operations
2 - Fundamental File Processing Operations
(CIS 256)
Yarmouk University
Department of Computer Information Systems
Note:
- Most of these slides have been prepared by Dr. Binnur Kurt from Istanbul Technical University, Computer
Engineering Department and adopted for our course with his permission.
-Additional slides have been added from the mentioned references in the syllabus.
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
#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;
}
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
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
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
– Multi-byte integers:
» Store high-order byte first or low-order byte first?
• Word size may affect file layout
– For a struct item, may allocate:
» 8-bytes (64-bit word)
» 4-bytes (32-bit word)
» 3-bytes (24-bit word)
• Different encodings for text
– ASCII
– EBCDIC
– Maybe other problems with international languages
hardware
• It is not a trivial matter
• Text offers some guidelines
– Agree on standard physical record format
• FITS is a good example
– Specifies physical format, keywords, order of keywords, bit
pattern for binary numbers
• Once get standard, stay with it
– Make the standard extensible
– Make it simple enough for wide range of machines, languages,
and OSs
Sun IBM
►Many Env’ts. – need an intermediate form:
IBM IBM
Sun Sun
.
.
.
XML
IA-32 IA-32