Presentation is loading. Please wait.

Presentation is loading. Please wait.

File Organization and Processing CS 215 2 nd Term 2010-2011 Basic file operations Cairo University Faculty of Computers and Information.

Similar presentations


Presentation on theme: "File Organization and Processing CS 215 2 nd Term 2010-2011 Basic file operations Cairo University Faculty of Computers and Information."— Presentation transcript:

1 File Organization and Processing CS 215 2 nd Term 2010-2011 Basic file operations Cairo University Faculty of Computers and Information

2 CS215 – File Structures - Sherif Khattab 2 2 nd 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

3 CS215 – File Structures - Sherif Khattab 3 2 nd 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)

4 CS215 – File Structures - Sherif Khattab 4 2 nd 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

5 CS215 – File Structures - Sherif Khattab 5 2 nd 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” :)

6 CS215 – File Structures - Sherif Khattab 6 2 nd 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?

7 CS215 – File Structures - Sherif Khattab 7 2 nd 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

8 CS215 – File Structures - Sherif Khattab 8 2 nd 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 *()

9 CS215 – File Structures - Sherif Khattab 9 2 nd 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

10 CS215 – File Structures - Sherif Khattab 10 2 nd Term 2010-2011 Feedback

11 CS215 – File Structures - Sherif Khattab 11 2 nd Term 2010-2011 Feedback

12 CS215 – File Structures - Sherif Khattab 12 2 nd Term 2010-2011 Feedback slides code samples course is easy, we need more

13 CS215 – File Structures - Sherif Khattab 13 2 nd 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) – 1 st method: lowest-level (more details) – 2 nd method: a little less details – 3 rd method: more details are hidden

14 CS215 – File Structures - Sherif Khattab 14 2 nd Term 2010-2011 1 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

15 CS215 – File Structures - Sherif Khattab 15 2 nd Term 2010-2011 1 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)

16 CS215 – File Structures - Sherif Khattab 16 2 nd Term 2010-2011 2 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

17 CS215 – File Structures - Sherif Khattab 17 2 nd Term 2010-2011 3 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

18 CS215 – File Structures - Sherif Khattab 18 2 nd Term 2010-2011 program to display file contents

19 CS215 – File Structures - Sherif Khattab 19 2 nd Term 2010-2011 1 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; }

20 CS215 – File Structures - Sherif Khattab 20 2 nd Term 2010-2011 2 nd method

21 CS215 – File Structures - Sherif Khattab 21 2 nd Term 2010-2011 3 rd method

22 CS215 – File Structures - Sherif Khattab 22 2 nd 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!


Download ppt "File Organization and Processing CS 215 2 nd Term 2010-2011 Basic file operations Cairo University Faculty of Computers and Information."

Similar presentations


Ads by Google