1 / 30

Chap2. Fundamental File Processing Operations

File Structures by Folk, Zoellick, Riccardi. Chap2. Fundamental File Processing Operations. 서울대학교 컴퓨터공학부 객체지향시스템연구실 SNU-OOPSLA-LAB 김 형 주 교수. Chapter Objectives. Describe the process of linking a logical file within a program to an actual physical file or device

tybalt
Download Presentation

Chap2. Fundamental File Processing Operations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. File Structures by Folk, Zoellick, Riccardi Chap2. Fundamental File Processing Operations 서울대학교 컴퓨터공학부 객체지향시스템연구실 SNU-OOPSLA-LAB 김 형 주 교수 SNU-OOPSLA Lab.

  2. Chapter Objectives • Describe the process of linking a logical file within a program to an actual physical file or device • Describe the procedures used to create, open, and close files • Introduce the C++ input and output classes • Explain the use of overloading in C++ • Describe the procedures used for reading from and writing to files • Introduce the concept of position within a file and describe procedures for seeking different positions • Provide an introduction to the organization of the UNIX file systems • Present the UNIX view of a file, and describe UNIX file operations and commands based on this view SNU-OOPSLA Lab.

  3. Contents 2.1 Physical files and Logical files 2.2 Opening files 2.3 Closing files 2.4 Reading and Writing 2.5 Seeking 2.6 UNIX directory structure 2.7 Physical and Logical files 2.8 File-related header files 2.9 UNIX file system commands SNU-OOPSLA Lab.

  4. Physical files and Logical files • Physical file • a file that actually exists on secondary storage • a file as known by OS and that appears in file directory • Logical file • a file as seen by program • allow program to describe operations to be performed on file not knowing what physical file will be used SNU-OOPSLA Lab.

  5. Connections bet. Physical files and Logical files • Main frame era: by Job Control Language • Unix and DOS era: by the instructions within the program (O/S system calls or parts of PLs) • ex) select inp_file assign to “myfile.dat” • inp_file: logical file, myfile.dat: physical file SNU-OOPSLA Lab.

  6. Opening files • Two options • open existing file • create new file, deleting any existing contents in the physical file • ex) fd = open(filename, flags [pmode]); • fd: file descriptor • filename: physical file name • flags: O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY • pmode: in O_CREAT, pmode is required protection mode SNU-OOPSLA Lab.

  7. Opening files: Example • Pmode = 0751 = 111 101 001 {owner, group, world X read, write, execute} • fd = open(fname, O_RDWR|O_CREAT,0751) • fd = open(fname,O_RDWR|O_CREAT|O_TRUC,0751) • fd = open(fname, O_RDWR|O_CREAT|O_EXCL,0751) SNU-OOPSLA Lab.

  8. Closing files • Files are closed automatically by the OS when a program terminate normally • CLOSE statement is needed only as protection against data loss in the event of program interruption and to free up logical filenames for reuse • ex) close(fd); (fd : file descriptor) SNU-OOPSLA Lab.

  9. Reading & Writing(1) • Input / Output operation • low-level system call (Unix) • read(Source_file, Destination_addr, Size) • write(Destination_file, Source_addr, Size) • C streams (in stdio.h) • file = fopen(filename, type); • fread, fget, fwrite, fput, fscanf, fprintf SNU-OOPSLA Lab.

  10. Reading & Writing(2) • Input/Output operations(cont’d) • C++ stream classes (in iostream.h, fstream.h) • fstream() • fstream(char *fname, int mode); • int open(char *fname, int mode); • int read(unsigned char *dest_addr, int size); • int write(unsigned char *source_addr, int size); • modes: ios::in, ios::out, ios::nocreate, ios::noreplace, ios::binary SNU-OOPSLA Lab.

  11. The File Listing Program Using C Streams // list.cpp // program using C streams to read characters from a // file and write them to the terminal screen #include <stdio.h> main() { char ch; FILE * file; // pointer to file descriptor char filename[20]; printf(“Enter the name of the file : “); gets(filename); file = fopen(filename, “r”); while (fread(&ch, 1, 1, file) != 0) fwrite(&ch, 1, 1, stdout); fclose(file); } SNU-OOPSLA Lab.

  12. The File Listing Program Using C++ Stream Classes // listcpp.cpp // list contents of file using C++ stream classes #include <fstream.h> main() { char ch; fstream file; // declare unattached fstream char filename[20]; cout << “Enter the name of the file : “ << flush; // force output cin >> filename; file.open(filename, ios::in); file.unsetf(ios::skipws); // include write space in read while(1){ file >> ch; if (file.fail()) break; cout << ch; } file.close(); } SNU-OOPSLA Lab.

  13. Reading & Writing(3) • Detecting the end of file • In C, fread() returns zero when reached the end of file • In C++, fstream::fail() return true if the previous operation on the stream failed • In Ada, end_of_file function is called before trying to read the next byte SNU-OOPSLA Lab.

  14. Seeking • moving directly to a certain position in a file • lseek(Source_file, Offset, Origin) in Unix • Offset - the pointer moved from the start of the source_file • File is viewed as an array of byte in C • pos = fseek(fd, byte_offset, origin) • In C++, An object of type fstream has two pointers • seekg() moves the get pointer, seekp() moves the put pointer • file.seekg(373, ios::beg), file.seekp(373, ios::beg) SNU-OOPSLA Lab.

  15. UNIX Directory Structure • UNIX file system : tree organization • File : identified by its absolute value • ex) /usr/mydir/addr • ‘.’ current directory, ‘..’ the parent of ‘.’ / (root) bin usr usr6 dev mydir bin lib lib console kbd TAPE adb cc yacc libdf.a DF addr libc.a libm.a SNU-OOPSLA Lab.

  16. Physical and Logical files in UNIX • UNIX views both physical devices and disk files as file. • ex) Keyboard(STDIN), Console(STDOUT), Error-file(STDERR) • I/O redirection, pipes • Shortcuts for switching between standard I/O and regular file I/O • ex) list > myfile /* I/O redirection */ • ex) program1 | program2 /* pipe */ SNU-OOPSLA Lab.

  17. Unix File System • File-related header files • /usr/include • In C, stdio.h • In C++, iostream.h and fstream.h • In Unix, fcntl.h and file.h • Unix file system commands • cat, tail, cp, mv, rm • chmod, ls, mkdir, rmdir SNU-OOPSLA Lab.

  18. File I/O in Standard Pascal(1) • included in language definition • provide high-level access to reading/writing • In C, a file is a sequence of bytes, but in Pascal, a file is a sequence of “records” • file I/O functions in Pascal • assign(input_fil, ‘myfile.dat’); // associate between a logical file(input_fil) and a physical file(‘myfile.dat’) • reset(input_fil); // open existing file • rewrite(input_fil); // create new file SNU-OOPSLA Lab.

  19. File I/O in Standard Pascal(2) • append(input_fil); // open to add data to existing file • read(input_fil, var); // read from file to variable • readln(input_fil, var); // read from file to variable • write(input_fil, var); // write from variable to file • writeln(input_fil, var); // write from variable to file • close(input_fil); // close file SNU-OOPSLA Lab.

  20. File I/O in C • Low-level I/O • UNIX system calls • fd1 = open(filename1, rwmode); • fd2 = open(filename2, rwmode); • read(fd1, buf, n); • write(fd2, buf, n); • lseek(fd1, offset, origin); • close(fd1); close(fd2); SNU-OOPSLA Lab.

  21. <stdio.h> • fp = fopen(s, mode) /* open file s; mode “r”, “w”, “a” for read, write, append (returns NULL for error) */ • c = getc(fp) /* get character; getchar() is getc(stdin) */ • putc(c, fp) /* put character; putchar(c) is putc(c, stdout) */ • ungetc(c, fp) /* put character back on input file fp; at most 1 char can be pushed back at one time */ • scanf(fmt, a1, ....) /* read characters from stdin into a1, ... according to fmt. Each ai must be a pointer. Returns EOF or number of fields converted */ • fscanf(fp, .....) /* read from file fp */ • printf(fmt, a1, ....) /* format a1, ... according to fmt, print on stdout */ • fprintf(fp, ....) /* print .... on file fp */ • fgets(s, n, fp) /* read at most n characters into s from fp. Returns NULL at end of file */ • fputs(s, fp) /* print string s on file fp */ • fflush(fp) /* flush any buffered output on file fp */ • fclose(fp) /* close file fp */ SNU-OOPSLA Lab.

  22. File I/O in C++ • #include <fstream.h> • File Stream: fstream, ifstream, ofstream • ex) ifstream f1(“input.fil”); • ofstream f2(“output.fil”, ios::out|ios::nocreat); • fstream f3(“inout.fil”, ios::in|ios::out); • f1.get(ch); f1.eof(); f2.put(ch); f2.bad(); • f1.seekg(); f2.seekp(); • f3.close(); SNU-OOPSLA Lab.

  23. <iostream.h> (1) class ios class istream: virtual public ios class ostream: virtual public ios class iostream: public istream, public ostream class hierarchy SNU-OOPSLA Lab.

  24. class ostream: virtual public ios { public: ostream& put(char); ostream& write(char*, int); ostream& seekp(int); ostream& operator<<(char); ostream& operator<<(int); ostream& operator<<(char*); ostream& operator<<(long); ostream& operator<<(short); ostream& operator<<(float); .............. }; class istream: virtual public ios { public: istream& get(char*, int, char = ‘\n’); istream& get(char); istream& read(char*, int); istream& gets(char**, char = ‘\n’); istream& seekg(int); istream& operator>>(char&); istream& operator>>(int&); istream& operator>>(char*); istream& operator>>(long&); ............... }; <iostream.h> (2) SNU-OOPSLA Lab.

  25. <iostream.h> (3) • class iostream: public istream, public ostream • { • public: • iostream( ) { } • }; SNU-OOPSLA Lab.

  26. Copy program in C++(1) #include <fstream.h> #include <libc.h> void error(char *s, char *s2 = ““){ cerr << s << ‘ ‘ << s2 << ‘\n’; exit(1); } int main(int argc, char *argv[]) { if( argc != 3) error(“wrong number of arguments”); ifstream src(argv[1]); //input file stream if (!src) error(“cannot open input file”, argv[1]); SNU-OOPSLA Lab.

  27. Copy program in C++(2) ofstream dest(argv[2]); //output file stream if(!dest) error(“cannot open output file”, argv[2]); char ch; while( src.get(ch) ) dest.put(ch); if(!src.eof() || dest.bad()) error(“something strange happened”); return 0; } SNU-OOPSLA Lab.

  28. Other Set: Primitive Operations • READ-NEXT(filename, buffer); • READ-DIRECT(filename, buffer, record-nbr); • UPDATE(filename, buffer); • APPEND(filename, buffer); • WRITE-NEXT(filename, buffer); • WRITE-DIRECT(filename, buffer, record-nbr); • logical EOF(filename); • logical VALID(filename); • ALLOCATE(filename, pointer); • DEALLOCATE(filename, pointer); SNU-OOPSLA Lab.

  29. File Functions • File functions we will develop from the primitive operations • Add - add a new record to a file • Delete - remove a record from a file • Read all records in key order • Read a record with specific key value • Update a record in a file • File reorganization SNU-OOPSLA Lab.

  30. Let’s Review!! 2.1 Physical files and Logical files 2.2 Opening files 2.3 Closing files 2.4 Reading and Writing 2.5 Seeking 2.6 UNIX directory structure 2.7 Physical and Logical files 2.8 File-related header files 2.9 UNIX file system commands SNU-OOPSLA Lab.

More Related