File Organization and Processing CS 215 2 nd Term 2010-2011 Basic file operations Cairo University...
-
Upload
buddy-davidson -
Category
Documents
-
view
212 -
download
0
Transcript of File Organization and Processing CS 215 2 nd Term 2010-2011 Basic file operations Cairo University...
File Organization and Processing
CS 215 2nd Term 2010-2011
Basic file operationsBasic file operations
Cairo University
Faculty of Computers and Information
CS215 – File Structures - Sherif Khattab 22nd Term 2010-2011
course objectives• learn how to use files in your programs
efficiently• understand how database management
systems (e.g., oracle, mysql, sql server) work• learn fundamental problems in computing and
analyze and evaluate their alternative solutions
CS215 – File Structures - Sherif Khattab 32nd Term 2010-2011
Why file structures?
• get data from files in as few disk accesses as possible
– disk access is about 1 million times slower than RAM access
• increase the possibility of caching (i.e., reusing the data you fetch from disk into RAM)
CS215 – File Structures - Sherif Khattab 42nd Term 2010-2011
Book approach
• study the history of file structure design (since 1970)
– analyze the problems faced and solutions proposed
• history repeats itself :) most of the file design problems that you will face are similar to old problems
• develop two toolkits– conceptual: the common fundamental solutions– object-oriented: a set of c++ classes to
implement these solutions
CS215 – File Structures - Sherif Khattab 52nd Term 2010-2011
object-oriented design review
what’s wrong in the above example?– yes, class variables should go into the private
section!– the book is somehow “old” :)
CS215 – File Structures - Sherif Khattab 62nd Term 2010-2011
object-oriented design review
class has a pointer, hence the big three are there (destructor, copy constructor, and assignment operator).
– can you write the implementation of these functions?
CS215 – File Structures - Sherif Khattab 72nd Term 2010-2011
conversion operator
• we have seen operator overloading of assignment (=), arithmetic (+, -, *, etc.), and boolean (<, >, ==, etc.) operators
• type conversion operators are needed to convert from the class type to other data types
without a conversion operator from String to char *, the third line in the above code gives a compilation error
– strcpy function expects two char * parameters
CS215 – File Structures - Sherif Khattab 82nd Term 2010-2011
conversion operator
operator char * is a type conversion operator from String to char *
• the implementation returns a copy (using strdup function) of the string private variable
• in modern c++ compilers, the operator has no return
– operator char *() instead of char * operator char *()
CS215 – File Structures - Sherif Khattab 92nd Term 2010-2011
physical and logical files
• phone sets = file handles in the program (e.g., fstream objects)
• the number of files that a program opens concurrently (in the same time) is limited
CS215 – File Structures - Sherif Khattab 102nd Term 2010-2011
Feedback
CS215 – File Structures - Sherif Khattab 112nd Term 2010-2011
Feedback
CS215 – File Structures - Sherif Khattab 122nd Term 2010-2011
Feedback
• slides• code samples• course is easy, we need more
CS215 – File Structures - Sherif Khattab 132nd Term 2010-2011
3 ways to open, read/write, and close files
• in c++, there are 3 ways that a program can use files:
– open, read or write, and close• each method provides a certain level of
abstraction (hiding of details)– 1st method: lowest-level (more details)– 2nd method: a little less details– 3rd method: more details are hidden
CS215 – File Structures - Sherif Khattab 142nd Term 2010-2011
1st method (fcntl.h)
• using integer file descriptors– the integer represent the phone set number :)– reading, writing, and closing use that integer
not the file name• flags tell the operating system how you want
your file opened
CS215 – File Structures - Sherif Khattab 152nd Term 2010-2011
1st method
• flags can be combined by bit-wise ORING (the | operator)
• the protection mode tells the operating system who and how users can access your file (permissions)
CS215 – File Structures - Sherif Khattab 162nd Term 2010-2011
2nd method (stdio.h)
• 2nd method is called C streams• the file descriptor integer is encapsulated in a
sturcture named struct_iobuf or FILE• the type string encapsulates flags of the 1st
method
CS215 – File Structures - Sherif Khattab 172nd Term 2010-2011
3rd method (fstream.h)
• 3rd method is called c++ streams• file descriptor is encapsulated in fstream
object• mode defined in the ios class encapsulates
the flags– ios::in, ios::out, ios::noreplace, ios::binary,
ios::nocreate
CS215 – File Structures - Sherif Khattab 182nd Term 2010-2011
program to display file contents
CS215 – File Structures - Sherif Khattab 192nd Term 2010-2011
1st method#include <fcntl.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
int main( ) {
char ch;
int file; // file descriptor
char filename[20];
printf("Enter the name of the file: "); // Step 1
fgets(filename, 20, stdin); // Step 2
filename[strlen(filename)-1] = 0;
file = open(filename, O_RDONLY); // Step 3
while (read(file, &ch, 1) != 0) // Step 4a
printf("%c", ch); // Step 4b
close(file); // Step 5
return 0;
}
CS215 – File Structures - Sherif Khattab 202nd Term 2010-2011
2nd method
CS215 – File Structures - Sherif Khattab 212nd Term 2010-2011
3rd method
CS215 – File Structures - Sherif Khattab 222nd Term 2010-2011
comments
• gets is unsafe. why?– use fgets– safer but?
• the return type of the main function• check that the file was opened correctly• check code samples on the course web site!