Download presentation
Presentation is loading. Please wait.
1
Chapter 9 – File Input Output
Introduction to pointers stream buffer C language stream interface C stream library Reading and writing data to file 1
2
What is a pointer? Why use pointers?
pointers are different from other variables that we have defined previously in that they contain ONLY the address (location in memory) of a variable. pointers are unsigned quantities! used for simulated pass by reference. used to dynamically allocate memory. used to interface with embedded controllers
3
POINTER declaration pointers point to specific data types ONLY
an asterisk in front of a variable name (identifier) indicates that it is a pointer: char *ptr_c int *ptr_i float * ptr_f double *henry
4
POINTER comments Pointers take up the same amount of memory, regardless of the data type they point to. usually 4 bytes in 32 bit machines Pointers can not be used to point to other data types (Why? wait for pointer arithmetic) Pointers follow usual initialization rules (and contain garbage if automatic scope). global pointers set to zero, which represents null
5
More POINTER comments the asterisk is associated with the name:
(1) char *ptr1, ptr2 (2) char* ptr1, *ptr2 (3) char *ptr1, *ptr2 prt1 is always a pointer, ptr2 is NOT a pointer in (1). approach (3) is preferred.
6
lvalues and rvalues Consider the declaration: int count = 5;
assume that count is stored in memory location 0x07A3. the rvalue is the data that the variable holds: in our case 5 the lvalue is the address where the data is stored: in our case 0x07A3.
7
POINTER initialization
From scanf, we know that placing an ampersand (&) in front of a variable means “address of ” (hence it is the address operator). We can use this idea to initialize pointers: int *count_ptr = &count; Now the rvalue of count_ptr is the lvalue of count (0x07A3) and the lvalue of count_ptr is the address of count_ptr.
8
Still more POINTER comments
Some compilers may only produce warnings if you initialize a pointer with the wrong data type. Pointers can be printed with %p (or %x) format. Pointers can be declared “register” variables. Pointers can not take the address of: register variables implicit constants expressions
9
indirection (deferencing) operator
a pointer can be used to retrieve the rvalue of the variable that it is pointing to with the indirection operator (*) WARNING: the asterisks serves three functions: multiplication, pointer definition and indirection. The indirection operator * can be thought of as inverse of address operator &
10
Program 9.1 - simple examples
/* Program #9.1, last modified Oct. 20, 1996 This is a simple demonstration of pointers */ #include <stdio.h> int main(void) { int count=5; int *count_ptr=&count; printf("for count , rvalue=%d; lvalue=%p\n",count,count_ptr); printf("for count , rvalue=%d; lvalue=%x\n",*count_ptr,&count); printf("for count , rvalue=%d; lvalue=%x\n",*&count,&*count_ptr); printf("for count_ptr, rvalue=%p; lvalue=%x\n",count_ptr,&count_ptr); return 0; }
11
Data Streams Goals: Approach:
to improve portability and reuse of codes. to select the input/output hardware at run time. Approach: communicate with peripheral devices in a uniform and consistent way. write input and output (I/O) functions, e.g. fscanf and fprintf, so that they are independent of the peripheral devices. write device drivers to interface between I/O and the actual devices.
12
Stream buffer and device interfaces
location in memory set aside to be transfer point to and from a device. Usually has a fixed size which depends on the device. (Stream) input buffer - when read occurs: data is input from buffer. When buffer is empty, more data is taken from device and placed in buffer. e.g. for a keyboard, buffer contains all keystrokes.
13
Stream buffers - continued
(Stream) output buffer - when write occurs: data is output to buffer. When buffer is full, data is written to device. Actually, there are 3 possibilities: full buffering (described above - good for disk files). often set by BUFSIZ - typically 256, 512, or 1024 bytes. line buffering: (data is written when a newline \n is encountered - good for printer). no buffering - (sometimes necessary)
14
C language stream interface
Recall the FILE data type that we used to declare a pointer for disk operations. FILE is a structure defined in <stdio.h> The details of structure are not important, though they are given on following slide. These structures usually reside within the operating system kernal (core program) in the stream I/O manager. Typically about 20 files can be opened, the first three are reserved for stdin, stdout, and stderr.
15
Typical FILE structure
#define MAX_FILE_NUM 20 typedef struct { int level; /* full or empty level of stream buffer */ unsigned flags; /* Stream status flag */ char path; /* Stream descriptor */ unsigned char hold; /* To hold unget char if no buffer */ int bsize; /* Size of buffer for stream */ unsigned char *buffer; /* Pointer to stream buffer */ unsigned char *nextc; /* Pointer to next char in buffer */ unsigned tempfile; /* Temporary file flag */ } FILE; extern FILE _iob[MAX_FILE_NUM ] /* located in OS kernal */
16
The C stream library We will describe some but definitely not all:
Stream creation and control: fopen(), freopen(), fclose(), fflush(), setbuf(), setvbuf(), tmpfile(); Stream read/write: fprintf(), fputs(), fputc(), fscanf(), fgets(), fgetc(), ungetc(), read(), write(); Stream positioning: fgetpos(), fsetpos(), fseek(), rewind(), ftell(); Stream error functions: feof(), ferror(), cleareff(), strerror(), perror(); miscillaneous: rename(), remove(), tmpnam();
17
fopen() - creating/opening a stream
fopen connects a stream with a physical device an element from _iob[] array is allocated to the program and a pointer of the type FILE is returned to it. A NULL pointer is returned if there is an error opening the file. The prototype for fopen in <stdio.h> is: FILE *fopen(const char *name, const char *mode); name is the pathlist / device name mode deals with allowed operations (read, write...)
18
Valid mode parameters for text files
“r” Open an existing file for reading only. “r+” Open an existing file for reading and writing. “w” Create a non existent file or open but truncate an existing file for writing. “w+” Create a non existent file file for writing & reading. “a” Open a file for appending at the end or create it if it doesn’t exist. “a+” Open a file for appending (for reading & writing).
19
Difference between r+, w+, & a+
these three modes all allow reading and writing but: for r+ an error occurs if file does not exist. for r+ the existing data is not discarded, but can be overwritten with random access. for w+ the existing data (if any) is always discarded. for a+ the existing data (if any) is not discarded and can not be overwritten, even with random access - data is always written at end of file.
20
SP indicators and EOF markers
The stream position indicator points to the next character in the stream. The end-of-file marker points points just past the (current) end of the file. for “r” mode SPI is set to the file beginning and EOF is set to the end. for “w” mode, SPI and EOF set to file beginning. for “a” mode, SPI and EOF set to file end.
21
fprintf() - formatted file print
function prototype in <stdio.h> : int fprintf(FILE *stream, const char *format, ...); returns the number of characters written to the stream or EOF for an error. NOTE: stream is typically buffered, and errors don’t usually occur until the device is accessed. Beware! stream is the file pointer returned by fopen. format is the printf compatible format string. the ellipsis indicates the arguments to be printed.
22
fscanf() - formatted file scan
function prototype in <stdio.h> : int fscanf(FILE *stream, const char *format, ...); returns the number of objects read from the stream or EOF for an error or end-of-file. EOF returned only if NO data succesfully read cause of error can be found from ferror() or feof(). error can be cleared with clearerr() or rewind(). stream is the file pointer returned by fopen. format is the printf compatible format string. the ellipsis indicates argument addresses to be scaned.
23
fclose() - closing a stream
function prototype in <stdio.h> : int fclose(FILE *stream); returns an EOF if error is encountered during close. any unread data in buffer is discarded any unwritten data is sent to device stream is the file pointer returned by fopen. releases _iob[] for use by another file
24
fflush() - flushing a stream
function prototype in <stdio.h>: int fflush(FILE *stream); returns an EOF if error is encountered during flush. any unread data in buffer is discarded. particularly useful when fscanf has an error. any unwritten data is sent to device. stream is the file pointer returned by fopen.
25
perror() function prototypes in <stdio.h> :
void perror(const char *message); perror prints the message to the default error stream (stderr)
26
Program part 1 /* write, append, and read values of sin(x) to disk */ #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <math.h> #define PI int main( void ) { FILE *f1, *fp1, *fp2; double count, temp_sin ;
27
Program 9.2 - part 2 /* write sin(x) from 0 to PI/2 */
if( (f1 = fopen( "sine.dat" , "w")) == NULL) { printf("Error cannot open file sine.dat for writing....." ) ; exit( EXIT_FAILURE ) ; } for( count = 0 ; count < PI/2 ; count += 0.01 ) { if( fprintf( f1 , "Sine of %6.3g radians = %8.6g\n" , count, sin( count )) ==EOF) { printf("Error writing out file sine.dat..... ") ; if( fclose( f1 ) ) printf("Error Closing File sine.dat.....") ;
28
Program 9.2 - part 3 /* append sin(x) from PI/2 to PI */
if( (f1 = fopen( "sine.dat" , "a")) == NULL) { printf("Error cannot open file sine.dat for appending....." ) ; exit( EXIT_FAILURE ) ; } for( count = PI/2 ; count < PI ; count += 0.01 ) { if( fprintf( f1 ,"Sine of %6.3g radians = %8.6g\n", count, sin( count )) ==EOF) { printf("Error appending out file sine.dat..... ") ; if( fclose( f1 ) ) printf("Error Closing File sine.dat.....") ;
29
Program part 4 /* read and print sin(x), cos(x) and tan(x) from 0 to PI (to another file)*/ if( (fp1 = fopen("sine.dat" , "r")) == NULL ) { printf("Error opening File sine.dat..... ") ; exit( EXIT_FAILURE ) ; } if( (fp2 = fopen("sctan.dat" , "w")) == NULL ) { printf("Error opening File sctan.dat .....") ;
30
Program 9.2 - final part while( 1 ) {
if( fscanf( fp1, "Sine of %lf radians = %lf\n" , &count, &temp_sin) != 2) break ; if( fprintf( fp2,"%f %f %f %f\n", count, temp_sin, cos(count), tan(count)) == EOF ) } if( ferror( fp1 )) printf("Error Reading File sine.dat.....") ; if( fclose( fp2 )) printf("Error Writing to File sctan.dat.....") ; return 0 ;
31
Program part 1 /* perform a bubble sort of an integer array from high to low*/ #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE *File_in, *File_out; int num[100], i, j, temp, count=0; // input all of the integers that we are going to sort if( (File_in = fopen( argv[1] , "r")) == NULL) { perror("Error cannot open file for reading....." ) ; exit( EXIT_FAILURE ) ; }
32
Program 9.3 - part 2 while( 1 ) {
if( fscanf( File_in, "%i\n" , &temp) != 1) break ; num[count++]=temp; } // print array before we start to sort printf("\n\nThe unsorted array is:\n"); for(i=0;i<count;i++) printf("%c %d",i%10?' ':'\n',num[i]);
33
Program 9.3 - part 3 // sort for(j=0;j<count-1;j++) {
for(i=j+1;i<count;i++) if(num[i]>num[j]) { temp=num[i]; num[i]=num[j]; num[j]=temp; } if( fclose( File_in ) ) perror("Error closing file.....") ;
34
Program 9.3 - part 4 if( fclose( File_in ) )
perror("Error closing file .....") ; if( (File_out = fopen( argv[2] , "w")) == NULL) { perror("Error cannot open file for writing....." ) ; exit( EXIT_FAILURE ) ; }
35
Program 9.3 - final part // finished sort high to low; print results
printf("\n\nThe SORTED array is:\n"); fprintf(File_out,"\n\nThe SORTED array is:\n"); for(i=0;i<count;i++) { printf("%c %d",i%10?' ':'\n',num[i]); fprintf(File_out,"%c %d",i%10?' ':'\n',num[i]); } printf("\n\n"); if( fclose( File_out ) ) perror("Error closing file.....") ; return 0;
36
fgets() - file string read
function prototype (in <stdio.h>): char *fgets (char *buff, int num, FILE *stream); buff points to the string read from stream. returns pointer to buff if successful; NULL else. fgets stops reading from stream either when a \n is encountered or after reading num characters.
37
fgets() - continued if num terminates string read, the next read from the stream starts where fgets stopped. No data from the end of the line is lost! fgets should typically be used instead of gets to avoid potential errors with invalid data. unlike gets, fgets places a null (\0) after the \n. for dynamic allocation, use sizeof(strlen(buff)+1) (Why?)
38
fputs() - file string write
function prototype (in <stdio.h>): int fputs (const char *buff, FILE *stream); buff points to the string written to the stream. returns zero if successful; EOF else. fputs differs from puts in that it does not append a \n at the end of the string. fputs should be used in conjunction with fgets.
39
fgetc() - file character read
function prototype (in <stdio.h>): int fgetc (FILE *stream); returns an integer (corresponding to a character) from the stream if successful, EOF else. returns an integer rather than a character to avoid potential problems of a comparison with EOF, which is an integer constant.
40
fputc() - file character write
function prototype (in <stdio.h>): int fputc (int c, FILE *stream); writes c to the stream. returns the (integer) c if successful, EOF else. returns an integer rather than a character to avoid potential problems of a comparison with EOF, which is an integer constant.
41
ungetc() - character return to stream
function prototype (in <stdio.h>): int ungetc (int c, FILE *stream); pushes the integer c (corresponding to a character) back onto the stream. returns the integer c if successful, EOF else. Why? sometimes you don’t know that you’ve read too far until it’s too late. Only one ungetc is guaranteed in ANSI C.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.