Understanding File-Oriented Input and Output in C Programming
170 likes | 288 Vues
This chapter explores file-oriented input and output in C programming, detailing how to store, update, and manage data files efficiently. It covers fundamental concepts such as text and binary files, file operations using functions like fopen, fscanf, fprintf, and fclose, along with file mode handling (read, write, append). The chapter emphasizes the importance of associating internal and external file names and provides practical examples and error handling tips for successful file operations. Mastering these techniques is crucial for effective data management in C.
Understanding File-Oriented Input and Output in C Programming
E N D
Presentation Transcript
Chapter 8 File-Oriented Input and Output
8.1 INTRODUCTION • a file can also be designed to store data. • We can easily update files, • A data file as input • Using a test editor to create data files
8.2 FUNDAMENTALS OF C DATA FILES • a data file is a named collection of records, normally kept in external storage. A record is the collection of related data values in a file. • A text file stores data as readable • A binary file consists of nonreadable characters • All records in a test file are terminated by an end-of-file marker placed by the operating system to mark the physical end of the file.
C Files and Streams • The most commonly used functions are • 1.The function fopen to open files, that is, to establish a stream between a program and a file • 2.The functions fscanf, fgetc, fgets, and fread for input of data from files • 3.The functions fprintf, fputc, fputs, and fwrite for output of data to a file • 4.The function feof to check for the end-of-file marker during input • The function fclose to close a file, or detach it from the program
Fscanf(student_file, “&d”, &test_score); • The prefix f reminder that they all operate on files.
Declaring Files • FILE *personnel_file; • Declares personnel_file as a file establishes a stream between your program and the file personnel_file • * declared as a pointer that is a variable contains the address of another variable or program entity in the main memory • The keyword FILE in a file declaration must be all uppercase.
Naming Data Files • Internal file name is the name that the C system uses to identify a file among others that a program might process. • External file name is the name that the operating system uses to identify a file in a disk directory • For example: • A:\PROSONNEL.TXT
Before we can process a file, we must properly associate the external and internal names for a file so that whenever our C program references the file, the operating system can understand which external file it should access for input or output purposes.
InternalFileName= fopen(ExternalFileName, OpenMode); • Used file open modes are • 1.“r”, can read data from it • 2.“w”, to write data to it • 3.“a”, append data to the end of an already existing file • 4.“r+”, opens a file for update(both an input and an output file) • 5.“w+”, destroys the file if it already exists and opens a new file for update
Open mode is important • 1.The OpenMode parameter in the fopen statement is required • 2.“w” and “w+” are destructive; will destroy that file and begin creating a new file • 3.“a” and “a+” are nondestructive. Can read data from a file that has been opened in “a+” • 4.Open mode of input “r” the file must exist physically
Example 8.2 • FILE *outdata; • Outdata=fopen(“a:OUTPUT.TXT”, “w”); • Establish outdata as an output stream between our program and the external file A:OUTPUT.TXT Example 8.3 • Char in_file_name[15];
Example 8.3 char in_file_name[15]; … FILE *indata; … printf(“Enter external file name for input file: “); gets(in_file_name); … indata=fopen(in_file_name, “r”);
File Open Verification • If an fopen function call returns a NULL pointer valus, then the call has failed. • NULL is a pointer variables. Exit(-1); • Will terminate the program execution and return the control to the operating system with a value of –1 • Abnormal program termination
Example 8.4 #include <stdlib.h> … if((indata=fopen(file_name, “r”)) ==Null) { printf(“File open error on %s”, file_name); exit(-1); }
fprintf(stderr, “File open error on %s”, file_name); • printing to stderr is faster than printing to stdout because the message appears on the screen without any delay or intermediate character buffering.
Closing Files and the fclose Function • Fclose(InternalFileName); • Cuts the connection between the program and the file. • The file pointer no longer exists • Fclose(indata); • Checking for End-of-File and the feof Function • To detect the end-of-file marker at the end of a data file
The feof function returns a nonzero integer(true) if the most recent input operation on the file InternalFileName has detected the thd-of-file marker; • For example, • While (! feof(student file)).