Download presentation
Presentation is loading. Please wait.
Published byClare Barton Modified over 9 years ago
1
Random File Access CHAPTER 7
2
C.12 2 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.
3
C.12 3 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).
4
C.12 4 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.
5
C.12 5 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.
6
C.12 6 Opening a Binary File » To open a file in binary mode, use one of the following as the opening mode in fopen(). ModeMeaning. rbRead wbWrite (Create) abAppend 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.
7
C.12 7 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.
8
C.12 8 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().
9
C.12 9 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: ValueSymbolic Equiv. Meaning. 0SEEK_SET Sets the FPI to the first character in the file 1 SEEK_CUR Does not alter the FPI, stays at its current position. 2SEEK_ENDSets the FPI to the end of the file
10
C.12 10 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”); 0 0 a a b b c c d d e e f f g g h h eof FPI
11
C.12 11 Examples fseek(input_file,3L,SEEK_CUR); 6 6 a a b b c c d d e e f f g g h h eof FPI 3 3 a a b b c c d d e e f f g g h h eof fseek(input_file,0L,SEEK_END); FPI 8 8 a a b b c c d d e e f f g g h h eof FPI fseek(input_file,-2L,SEEK_CUR);
12
C.12 12 Examples fseek(input_file,0L,SEEK_SET); 2 2 a a b b c c d d e e f f g g h h eof FPI 0 0 a a b b c c d d e e f f g g h h eof fseek(input_file,-3L,SEEK_END); FPI 5 5 a a b b c c d d e e f f g g h h eof FPI fseek(input_file,2L,SEEK_SET);
13
C.12 13 Examples fseek(input_file,8L,SEEK_SET); 6 6 a a b b c c d d e e f f g g h h eof FPI 8 8 a a b b c c d d e e f f g g h h eof fseek(input_file,-8L,SEEK_END); FPI 0 0 a a b b c c d d e e f f g g h h eof FPI fseek(input_file,6L,SEEK_CUR);
14
C.12 14 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) { … }
15
C.12 15 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);
16
C.12 16 Examples #include 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); // why?? because of a bug! ch = getc(input_file); } fclose(input_file); }
17
C.12 17 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.