Presentation is loading. Please wait.

Presentation is loading. Please wait.

Folk/Zoellick/Riccardi, File Structures 1 Objectives: To get familiar with Logical files vs. physical files File processing operations File processing.

Similar presentations


Presentation on theme: "Folk/Zoellick/Riccardi, File Structures 1 Objectives: To get familiar with Logical files vs. physical files File processing operations File processing."— Presentation transcript:

1 Folk/Zoellick/Riccardi, File Structures 1 Objectives: To get familiar with Logical files vs. physical files File processing operations File processing in UNIX/C File processing using C streams File processing using C++ stream classes UNIX file system Chapter 2 Fundamental File Processing Operations

2 Folk/Zoellick/Riccardi, File Structures 2 Physical Files vs. Logical Files Physical file: a file physically exists as a collection of bytes on a disk or a tape. Logical file: a file referred by an application program. –The application program can read/write bytes to the logical file. –The application program does not know where and how the file is stored in a disk or a tape. –The operating system takes care of the details of getting bytes from and putting bytes to a disk or a tape. –The application program gives instructions to the operating system to hook up a logical file to a physical file. –Some languages (COBOL) require explicit hook-up while others (C++) do it implicitly at file opening.

3 Folk/Zoellick/Riccardi, File Structures 3 File Processing Operations open -- make the file available for programs close -- unplug the file to make it unavailable for programs read -- read data from an opened file write -- write data to an opened file seek -- move the file pointer, i.e., the next position to read/write

4 Folk/Zoellick/Riccardi, File Structures 4 Opening Files in UNIX/C The UNIX system function open( ) is used to open an existing file or create a new file. fd = open(filename, flags, [pmode]); –fd: the file description -- the logical file name. The fd is an integer. If there is an error in the attempt to open the file, fd is negative (-1). –filename: the physical file name. The filename argument can be a pathname. –flags: an integer argument that controls the operation of the open function. The values of flag is set by performing a bitwise OR of the following values: »O_APPEND : Append every write operation to the end of the file. »O_CREAT : Create and open a file for writing. »O_EXCL : Return an error if O_CREAT opens an existing file. »O_RDONLY : Open a file for reading only. »O_RDWR : Open a file for reading and writing. »O_TRUNC : Truncate an existing file to a length of 0, destroying its contents. »O_WRONLY : Open a file for writing only. »and many others for synchronization.

5 Folk/Zoellick/Riccardi, File Structures 5 Opening Files in UNIX/C (cont’d) –pmode: An integer argument to specify the protection mode. »If O_CREAT is specified, pmode is required. »In UNIX, the pmode is a three-digit octal that indicates how the file can be used by the owner (1st digit), by members of the owner’s group (2nd digit), and by everyone else (3rd digit). r: read permission, w: write permission, e: execute permission. pmode = 751 = r w er w er w e 1 1 11 0 10 0 1 ownergroupworld »File protection is tied more to the operating system than to a specific language. –Examples: fd = open(filename, O_RDWR | O_CREAT, 0751); fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0751); fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0751);

6 Folk/Zoellick/Riccardi, File Structures 6 Closing Files in UNIX/C Finish use of a physical file: close(fd); The logical file name or file descriptor is available for use with another file. For an output file, ensures that everything has been written to the file. Files are usually closed automatically, when a program terminates normally. The explicit use of CLOSE statement is to protect against data loss in the event of program interruption or to reuse the logical file name. Some languages, including standard Pascal, do not provide a CLOSE statement. Some languages, including C, VAX Pascal, and PL/I, allow explicit file closing.

7 Folk/Zoellick/Riccardi, File Structures 7 Reading and Writing Files in UNIX/C Basic input/output operations for file processing. Generic READ( ) and WRITE( ) functions: READ(Source_file, Destination_addr, Size) WRITE(Destination_file, Source_addr, Size) –Source_file and Destination_file : logical file names. –Destination_addr and Source_addr : starting memory location for the read/written data. –Size : the number of bytes to be read/written. –Returns the actual number of bytes read/written. –READ and WRITE implicitly move the file pointer.

8 Folk/Zoellick/Riccardi, File Structures 8 Seeking in UNIX/C The seek function in UNIX/C: lseek( ) pos = lseek(fd, byte_offset, origin) –pos : a long integer value returned by lseek( ) equal to the number of bytes from the beginning to the file pointer after it has been moved. –fd : the file descriptor. –byte_offset : the number of bytes to move from some origin in the file. The byte_offset is a long integer and can be a negative value. –origin : a value that specifies the starting position from which the byte_offset is to be taken. The values of origin: »SEEK_SET: lseek( ) from the beginning of the file; »SEEK_CUR: lseek( ) from the current position; »SEEK_END: lseek( ) from the end of the file.

9 Folk/Zoellick/Riccardi, File Structures 9 C/C++ streams In C/C++, a file (and other devices like keyboard) is a stream of data. There are two sets of I/O operations. –C streams in stdio.h –C++ stream classes in iostream.h and fstream.h Comparison between UNIX/C operations and C/C++ streams –both support a complete set of file operations UNIX/C Available mostly on UNIX, (also in Microsoft Visual C++) Fast Low level C/C++ Streams Standard C/C++ features, available on almost all operating systems Provide structured I/O

10 Folk/Zoellick/Riccardi, File Structures 10 C Streams Three standard streams: stdin, stdout, and stderr. Opening file fopen(const char *filename, const char *mode) Closing file fclose(FILE *fp) Reading file fread(void *buf, size_t size, size_t num, FILE *fp) //read num items of size bytes into buf from fp fgetc(FILE *fp)// return the next character from fp fgets(char *buf, int size, FILE *fp) // read a line or up to size bytes into buf from fp fscanf(FILE *fp, const char *format, …) // read and format data from fp

11 Folk/Zoellick/Riccardi, File Structures 11 C Streams (Cont.) Writing file fwrite(const void *buf, size_t size, size_t num, FILE *fp) //write num items of size bytes from buf to fp fputc(int ch, FILE *fp) //write the character ch to fp fputs(const char *buf, FILE *fp) // write the string in buf to fp fprintf(FILE *fp, const char *format, …) // write formatted data to fp Seeking file fseek(FILE *fp, long offset, int origin)

12 Folk/Zoellick/Riccardi, File Structures 12 C++ Stream Classes C++ handles file I/O by creating objects of the stream classes. Standard stream objects: cin, cout, cerr, clog Stream classes: in file iostream.h: ios, istream, ostream, iostream, in file fstream.h: ifstream, ofstream, fstream ios istreamostream ifstreamiostreamofstream fstream

13 Folk/Zoellick/Riccardi, File Structures 13 C++ Stream Classes (Cont.) Opening file constructor member function open Closing file destructor member function close Reading file overloaded extracting operator << many others: read, get, getline Writing file overloaded inserting operator >> many others: write, put Seeking file seekg: set the read/get pointer seekp: set the write/put pointer

14 Folk/Zoellick/Riccardi, File Structures 14 The LIST Program A simple file processing program: LIST –Display a prompt for the name of the input file. –Read the user’s response from the keyboard into a variable called filename. –Open the file for input. –While there are still characters to be read from the input file, »read a character from the file and, »write the character to the terminal screen. –Close the input file.

15 Folk/Zoellick/Riccardi, File Structures 15 The LIST Program in UNIX/C /* read characters from a file and write them to the terminal screen */ #include main( ) { charc; intfd;/* file descriptor */ charfilename[20]; printf(“Enter the name of the file: “);/* step 1 */ gets(filename);/* step 2 */ fd = open(filename, O_RDONLY);/* step 3 */ while (read(fd, &c, 1) != 0)/* step 4a */ putchar(c); /* write(stdout, &c, 1); does not work step 4b */ close(fd);/* step 5 */ }

16 Folk/Zoellick/Riccardi, File Structures 16 The LIST Program using C Streams // listc.cpp // program using C streams to read characters from a file // and write them to the terminal screen #include main( ) { char ch; FILE * file; // file descriptor char filename[20]; printf("Enter the name of the file: ");// Step 1 gets(filename);// Step 2 file =fopen(filename, "r");// Step 3 while (fread(&ch, 1, 1, file) != 0)// Step 4a fwrite(&ch, 1, 1, stdout);// Step 4b fclose(file);// Step 5 }

17 Folk/Zoellick/Riccardi, File Structures 17 The LIST Program using C++ Stream Classes // listcpp.cpp // list contents of file using C++ stream classes #include void main () { char ch; fstream file; // declare fstream unattached char filename[20]; cout <<"Enter the name of the file: "// Step 1 <<flush; // force output cin >> filename;// Step 2 file.open(filename, ios::in);// Step 3 file.unsetf (ios::skipws);// include white space in read while (1) { file >> ch;// Step 4a if (file.fail()) break; cout << ch;// Step 4b } file.close();// Step 5 }

18 Folk/Zoellick/Riccardi, File Structures 18 Detecting End-of-File In UNIX/C –read returns 0 Using C streams –fread returns -1 –feof returns true Using C++ stream classes –fail returns true –eof returns true

19 Folk/Zoellick/Riccardi, File Structures 19 Special Characters in Files Some special characters are inserted, deleted, or modified in the files by the operating system. The use of special characters is determined by operating systems. Examples: –End-of-file: Control-Z (ASCII value of 26) is inserted at the end of file. –End-of-line: Some text files use a pair of carriage return (CR: ASCII value 13) and a line feed (LF: ASCII value 10) to indicate end-of-line. Some I/O procedures may automatically extend single CR or LF characters to CR-LF pairs. –Character count: Some VMS may remove carriage return characters and replace them with a count of the characters.

20 Folk/Zoellick/Riccardi, File Structures 20 UNIX File System The UNIX file system is a tree. –The root of the tree is the root directory. –Non-leaf nodes are directories. –Leaf nodes are files. UNIX treats all devices as files. –/dev/kdb –/dev/console –/dev/mouse I/O redirection and pipe –change stdin, stdout, stderr to other files


Download ppt "Folk/Zoellick/Riccardi, File Structures 1 Objectives: To get familiar with Logical files vs. physical files File processing operations File processing."

Similar presentations


Ads by Google