Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.

Similar presentations


Presentation on theme: "Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records."— Presentation transcript:

1 Introduction to Programming Using C Files

2 2 Contents Files Working with files Sequential files Records

3 3 Files Arrays can store many strings but – They are limited by the amount of memory in the computer – When your program stops the data is lost Files offer an alternative – Data is stored on disk – Available space is much larger – Data is retained even when your program stops

4 4 Files A file is – A named area on a disk – Stored as a stream of bytes – Can be read and/or written by programs A file can contain – Text data – Binary data

5 5 Working with Files To access a file – A program uses the function fopen() to open a file which establishes a connection to the file – The program uses the function fprintf() and fscanf() to write to the file and read from the file – The program uses the close() function to write any remaining data to the file and sever the connection to the file

6 6 Working with Files All of the file functions are declared in – #include When you open a file, it returns a data structure which references the file This must be stored as a file pointer – FILE *fp; After the file is opened, this variable will be used to indicate which file you want to access

7 7 The fopen() Function fopen() is the function which opens a file – FILE *fopen(char filename[], char mode[]); The first parameter is the name of the file The second parameter is the mode – “r” to read from the file and fail if the file does not exist – “w” to write to a new file or to overwrite the data in an existing file – “a” to append to the end of an existing file or to create a new file and append onto it

8 8 The fopen() Function If fopen() is successful, it returns – A pointer to a data structure describing the open file If unsuccessful, it returns – The value 0, defined as NULL in stdio.h We normally compare the value returned to NULL to make sure that the file opened correctly before we proceed

9 9 Sequential Files The files we will be dealing with are called sequential since – Data is written to them as a continuous stream – We read from the beginning of the file – We write data at the end of the file There is another type of file called random access that lets us read or write at any position in the file

10 10 fprintf() and fscanf() These functions write to and read from files They work just like printf and scanf except – They have an extra first parameter which points to the file they should write to or read from – fprintf(FILE *fp, char format[], data…); – fscanf(FILE *fp, char format[], data…); * See file_io_demo.c

11 11 fclose() This function – Closes a file when we no longer need it – Ensures that all data is on disk before the file is closed – Frees the data structures which reference the file – Might unlock the file on some operating systems – Should be called before your program terminates

12 12 Other File Functions rewind(fp) – Rewinds a file to the beginning so you can read it again c = fgetc(fp) – Reads a single character or returns -1 (EOF) if at end of file – It is best to declare c to be an int so that it is easy to compare to EOF

13 13 Other File Functions fgets(char buf[], int max, FILE *fp) – Reads an entire line from a file, including the newline character fputc(char ch, FILE *fp) – Writes a single character to a file fputs(char str[], FILE *fp) – Writes a string to a file

14 14 Records and Fields There are 2 types of files we use – Report files Designed to be viewed by people or printed out – Data files Stores data to be read by the same program or another program

15 15 Data Files Data files often repeat the same information over and over Consider a customer file – Customer number, – Customer name – Customer address – Customer balance This information is repeated for every customer

16 16 Data Files All of the information on one customer is called – A record Each piece of information on one employee is called – A field – Eg. The customer name field

17 17 Data Files There is no set way to organize such file but one way would be – Each record is on a line by itself – Each field is terminated by a semi-colon This would make our file look like this 2345;Bob Jones;94 Oak St;234.67; 9876;Joan Smith; 32 Elm St.;128.45 7744;Fran Fox;183 Pine Ave.;439.00

18 18 More Logical Operations There is one more logical operator – ! – Not – This simply reverses true to false and false to true Eg. – !(x < y) – Is the same as – x >= y


Download ppt "Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records."

Similar presentations


Ads by Google