File Organization and Processing CS nd Term Basic file operations Cairo University Faculty of Computers and Information
CS215 – File Structures - Sherif Khattab 2 2 nd Term 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 3 2 nd Term 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 4 2 nd Term 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 5 2 nd Term 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 6 2 nd Term 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 7 2 nd Term 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 8 2 nd Term 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 9 2 nd Term 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 10 2 nd Term Feedback
CS215 – File Structures - Sherif Khattab 11 2 nd Term Feedback
CS215 – File Structures - Sherif Khattab 12 2 nd Term Feedback slides code samples course is easy, we need more
CS215 – File Structures - Sherif Khattab 13 2 nd Term 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) – 1 st method: lowest-level (more details) – 2 nd method: a little less details – 3 rd method: more details are hidden
CS215 – File Structures - Sherif Khattab 14 2 nd Term st 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 15 2 nd Term st 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 16 2 nd Term nd method (stdio.h) 2 nd 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 1 st method
CS215 – File Structures - Sherif Khattab 17 2 nd Term rd method (fstream.h) 3 rd 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 18 2 nd Term program to display file contents
CS215 – File Structures - Sherif Khattab 19 2 nd Term st method #include 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 20 2 nd Term nd method
CS215 – File Structures - Sherif Khattab 21 2 nd Term rd method
CS215 – File Structures - Sherif Khattab 22 2 nd Term 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!