180 likes | 341 Views
Random File Access. CHAPTER 7. Text and Binary Files. While the concepts of “text files” given, we processed files sequentially, one character, integer at a time. Each program read a character, then read the next character, then the next character, and so on.
E N D
Random File Access CHAPTER 7
Text and Binary Files • While the concepts of “text files” given, we processed files sequentially, one character, integer at a time. • Each program read a character, then read the next character, then the next character, and so on. • It is also possible to process the characters in a file randomly. • Thus, a program can begin processing a file at any desired location within the file, then go to another location in the file, and so on at random.
Text and Binary Files • A text file is a sequence of characters that occasionally includes a new-line character. • In some operating systems, most notable DOS (also this is true for versions of the MS Windows), when a program places the new-line character into a file, the new-line character is physically stored two characters: • Carriage Return (CR) • Line Feed (LF) • So, the new-line character is also named as CRLF mark. • Similarly, when a program reads a CRLF pair, the system converts them to single new-line character (that are seen as a single character in the program).
Text and Binary Files • In Unix, Linux like operating systems, the new-line character is indicated only with LF (line feed) character, and there is no need to do any conversion. • When randomly processing a file, a program must know its exact position in the file. • If we open the file in text file format, it is not possible to process file randomly. • In text files, the new-line to CRLF conversions make it impossible to control the positioning in the file. • It is a need to open the file in binary mode, in order to process it randomly.
Text and Binary Files • When a program opens a file as a binary file, none of the characters is ever changed when read or written by the program. • In a binary file the CRLF pair is not accepted as a single new-line character, instead, these are accepted as a pair of characters. • Opening a file in binary mode is just needing to place the letter “b” at the end of the opening mode string in the fopen() statement.
Opening a Binary File • To open a file in binary mode, use one of the following as the opening mode in fopen(). Mode Meaning . rb Read wb Write (Create) ab Append r+b or rb+ Read and write w+b or wb+ Write (Create) and read a+b or ab+ Append and read Example: input_file = fopen(“TEST.TXT”, “rb”); • It opens the “TEST.TXT” file for reading in binary mode.
Opening a Binary File • Open a file in binary mode, when it must appear to the program exactly as it is stored. • That includes files that are to be : • Processed randomly • Executable files • Data files (image files (like gif, jpg), sound file (like wav, mp3), movie files (avi, mpeg). • In order to process a binary file randomly, you need to use the Random Access Functions.
Random Access Functions • Random access functions are used to manipulate the file’s position indicator, or FPI. • We will use getc(), and putc() functions for input/output purposes (to access the characters in a file). • In order to access the character on the desired location really we need a special function to manipulate the FPI. • The most useful direct access function in the C library is fseek().
Random Access Functions • The syntax of the fseek() function is: fseek(file_pointer, offset, location) • The third argument is one of three integer values. • The third argument determines the value that the function places initially in the file’s FPI. • The possible values of the third argument can be: Value Symbolic Equiv. Meaning . 0 SEEK_SET Sets the FPI to the first character in the file 1 SEEK_CUR Does not alter the FPI, stays at its current position. 2 SEEK_END Sets the FPI to the end of the file
Random Access Functions • The second argument in fseek() is the offset, which must be a long integer. • After setting the FPI by using the third argument, fseek() then adds the value of the second argument to the FPI. • It carries the FPI to a position that is far the value of the second argument away from the main position given in the first argument. • Examples: fopen(“TEST.TXT”,”rb”); FPI 0 a b c d e f g h eof
Examples fseek(input_file,3L,SEEK_CUR); FPI 3 a b c d e f g h eof fseek(input_file,0L,SEEK_END); FPI 8 a b c d e f g h eof fseek(input_file,-2L,SEEK_CUR); FPI 6 a b c d e f g h eof
Examples fseek(input_file,0L,SEEK_SET); FPI 0 a b c d e f g h eof fseek(input_file,-3L,SEEK_END); FPI 5 a b c d e f g h eof fseek(input_file,2L,SEEK_SET); FPI 2 a b c d e f g h eof
Examples fseek(input_file,8L,SEEK_SET); FPI 8 a b c d e f g h eof fseek(input_file,-8L,SEEK_END); FPI 0 a b c d e f g h eof fseek(input_file,6L,SEEK_CUR); FPI 6 a b c d e f g h eof
Examples • To detect the End-of-File position on a file, the feof() function can be used. Syntax : int feof( FILE *file-pointer); • The function returns a nonzero value if the end of file has been detected. Ex.: while (!feof(file_ptr) { … }
Examples The READ AHEAD RULE: • Read sequentially a file until you reach the end of file. ex.: FILE *input_file; char ch; input_file = fopen(“TEST.TXT”,”rb”); ch=getc(input_file); while (!feof(input_file)) { printf(“%c”,ch); ch=getc(input_file); } fclose(input_file);
Examples #include <stdio.h> #include <stdlib.h> #include <ctype.h> void main() { FILE *input_file; char ch; input_file= fopen(“EXAMPLE.TXT”,”rb+”); ch = getc(input_file); while (!feof(input_file) { fseek(input_file, -1L, SEEK_CUR); putc(toupper(ch),input_file); fseek(input_file,0L, SEEK_CUR); ch = getc(input_file); } flose(input_file); }
Examples • Recall that, executing a getc() obtains a character and moves the FPI to the next character. • Also, putc() moves the FPI to the next character after the character is printed. • So, on the previous example, it was needed to bring the FPI to back to the current position with “-1L” offset. • On rewrite operations, after the read operation, you have to bring the FPI on to the same position to rewrite the modified version of the read character.