EEE 0115 Chapter 11 C File Processing 1

  • Slides: 15
Download presentation
EEE 0115 Chapter 11: C File Processing 1 C How to Program Deitel &

EEE 0115 Chapter 11: C File Processing 1 C How to Program Deitel & Deitel

2 Outline Data Hierarchy Files and Streams Sequential-Access Files Random-Access Files

2 Outline Data Hierarchy Files and Streams Sequential-Access Files Random-Access Files

3 Data Hierarchy Bit 0 or 1 Byte 8 bits Used Field to store

3 Data Hierarchy Bit 0 or 1 Byte 8 bits Used Field to store a character – Group of characters that has meaning Record – Group of related fields File – Group of related records Database – Group of related files

4 Files and Streams Each file can be considered as a sequence of bytes.

4 Files and Streams Each file can be considered as a sequence of bytes. Files ends with end-of-file marker When a file is opened, a pointer to a FILE structure returned. Example file pointers: stdin, stdout, stderr

5 Files and Streams Read/Write functions in standard library fgetc Reads one character from

5 Files and Streams Read/Write functions in standard library fgetc Reads one character from a file FILE pointer is given as an argument fgetc(stdin) is same as getchar() fputc Writes one character to a file FILE pointer and a character are given as an argument fputc(‘a’, stdout) is same as putchar(‘a’)

6 Files and Streams Read/Write functions in standard library fgets Reads a line from

6 Files and Streams Read/Write functions in standard library fgets Reads a line from a file fputs Writes a line to a file fscanf/fprintf File processing equivalents of scanf and printf

7 Sequential Access Files C does not enforce a file structure Programmer Creating must

7 Sequential Access Files C does not enforce a file structure Programmer Creating must specify the file structure a file FILE *file. Ptr; creates a file pointer file. Ptr = fopen(“students. dat", “w”); fopen function returns a FILE pointer if the file is opened successfully otherwise it returns NULL fopen function takes two arguments, file name and file open mode

8 Sequential Access Files fprintf function is used to print to a file It

8 Sequential Access Files fprintf function is used to print to a file It is like printf, it has only one difference; it takes additional first argument namely a FILE pointer feof takes a FILE pointer as an argument and it returns true if the end of file indicator is set fclose takes a FILE pointer as an argument and closes the given file.

9 Sequential Access Files

9 Sequential Access Files

10 Sequential Access Files To read from a sequential access file, you need to

10 Sequential Access Files To read from a sequential access file, you need to create a FILE pointer and link it to an existing file Function fscanf is used from the file File position pointer is an important concept here. It indicates number of next byte to be read/written. Function rewind takes a FILE pointer as an argument and reposition the file position pointer to beginning of file.

Outline 11 Sequential Access Files fig 11_07. c (1 of 2 ) fopen function

Outline 11 Sequential Access Files fig 11_07. c (1 of 2 ) fopen function opens a file; r argument means the file is opened for reading

12 Sequential Access Files

12 Sequential Access Files

13 Random Access Files Individual records can be accessed without searching through other records.

13 Random Access Files Individual records can be accessed without searching through other records. Instant access Data can be recorded without destroying other data. Implemented using fixed length records.

14 Random Access Files Data in random access files is unformatted. Not human readable

14 Random Access Files Data in random access files is unformatted. Not human readable All data of the same type uses the same amount of memory.

15 Random Access Files Unformatted I/O functions fwrite( &my. Object, sizeof (struct my. Struct),

15 Random Access Files Unformatted I/O functions fwrite( &my. Object, sizeof (struct my. Struct), 1, my. Ptr ); fread( &client, sizeof (struct client. Data), 1, my. Ptr ); fseek Sets file position pointer to a specific position fseek( pointer, offset, symbolic_constant );