Presentation is loading. Please wait.

Presentation is loading. Please wait.

Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei

Similar presentations


Presentation on theme: "Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei"— Presentation transcript:

1 Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei

2 Working with files What a file is in C How files are processed
How to write and read formatted files and binary files How to use temporary work files in a program Beiginning C / Qinpei Zhao 2018/11/29

3 a file A file is essentially a serial sequence of bytes File streams
Text file: written as characters organized as lines Binary file: written as a series of bytes Regardless of the file type, it ends up as just a series of bytes (e.g., 12 bytes in a binary file could be 12 characters, 12 8-bit signed integers, 12 8-bit unsigned integers, 6 16-bit signed integers, a 32-bit integer) Beiginning C / Qinpei Zhao 2018/11/29

4 Accessing a file - open When you process a file in C, your program references a file through a file pointer. open a file by calling the standard library function fopen() double quote Returns the file pointer for a specific external file Beiginning C / Qinpei Zhao 2018/11/29

5 Accessing a file - open A call to fopen() does two things:
creates a file pointer that identifies the specific file on disk that your program is going to operate on Determines what you can do with the file within your program FILE specifies a structure type that has been predefined in the header file <stdio.h> through a typedef. There’s a limit to the number of files you can open at one time (FOPEN_MAX) What’s the FOPEN_MAX on your computer? Beiginning C / Qinpei Zhao 2018/11/29

6 Accessing a file - open Create a new text file! opens the file and associates the file “myfile.txt” with pointer pfile. “w” indicates you can only write, can’t read. The first argument is limited to FILENAME_MAX If the file does not exist, it will create a new file with this name. If the call to fopen() does fail for any reason, NULL will be returned. The file will be overwritten if the file is positioned at the beginning of any existing data for the first operation. Beiginning C / Qinpei Zhao 2018/11/29

7 Accessing a file - rename
The integer that’s returned will be 0 if the renaming is successful, and nonzero otherwise. The file must be closed when you call rename(), otherwise the operation will fail. Note the double backslash ( \\ ) in the file path string. Beiginning C / Qinpei Zhao 2018/11/29

8 Accessing a file - close
When you’ve finished with a file, you need to tell the operating system to free up your file pointer. Calling the fclose() function which accepts a file pointer as an argument and returns a value of type int EOF if an error 0 otherwise EOF: end-of-file character (usually = -1) It’s good programming practice to close a file as soon as you’ve finished with it. You must also close a file before attempting to rename or remove it. Beiginning C / Qinpei Zhao 2018/11/29

9 Accessing a file - delete
fflush(): force any unwritten data left in a buffer to be written to a file. (flush the input buffer) delete the file from the current directory that has the name pfile.txt. Take particular care with the operations that delete files. Beiginning C / Qinpei Zhao 2018/11/29

10 Writing to a text file fputc(): writes a single character to a text file Buffer Beiginning C / Qinpei Zhao 2018/11/29

11 Reading from a text file
fgetc(): reads a character from a text file that has been opened for reading mchar is type int getc() v.s. gets() getc(): reads a single character from the stream specified by its argument. gets(): reads a whole line of input from a standard input stream, which is the keyboard lec11_1_usefile.c Beiginning C / Qinpei Zhao 2018/11/29

12 Writing strings to a text file
fputs(): it continues to write characters from a string until it reaches a ‘\0’ character, which is not written to the file. char *pstr: a pointer to the character string that’s to be written to the file. FILE *pfile: a file pointer. Example: Beiginning C / Qinpei Zhao 2018/11/29

13 Reading strings from a text file
fgets(): reading a string from a text file. char *pstr: read a string into the memory area pointed to by pstr. FILE *pfile: a file pointer. Characters are read from the file until either a ‘\n’ is read or nchars-1 characters have been read from the file. lec11_2_stringfile.c Beiginning C / Qinpei Zhao 2018/11/29

14 Dealing with errors Write error messages to stderr
The output will always be directed to the display and it will always be written immediately to the display device. Terminating a program by calling exit() ensures that output stream buffers will be flushed. Always include some basic error checking and reporting code in all of your programs. Beiginning C / Qinpei Zhao 2018/11/29

15 Binary file input and output
binary mode: no data is transformed or precision lost, faster than text mode. Characters such as ‘\n’ and ‘\0’ that have specific significance in text mode are of no consequence in binary mode. Beiginning C / Qinpei Zhao 2018/11/29

16 Writing & reading a binary file
fwrite(): writing a specified number of binary data items to a file. pdata, a pointer containing the starting address in memory of where the data items to be written are stored. The second argument specifies the size in bytes of each item to be written num_items, defines a count of the number of items to be written to the file The file to which the data is to be transferred is identified by pfile. There is no check that you opened the file in binary mode when you call the fwrite() function. Beiginning C / Qinpei Zhao 2018/11/29

17 File modes Beiginning C / Qinpei Zhao 2018/11/29


Download ppt "Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei"

Similar presentations


Ads by Google