PDF Google Drive Downloader v1.1


Báo lỗi sự cố

Nội dung text Unit VIII - File Handling (File Input-output) – C Programming.pdf

www.ckundan.com.np 1 Unit VIII: File Handling (File Input/output) – C Programming Data Files: The input output function, like printf() scanf(), getchar(), putchar(), gets(), puts() are known as console oriented I/O functions, which always use keyboard for input device. While using these library functions, the entire data is lost when either the program is terminated or the computer is turned off. At the same time, it is cumbersome and time consuming to handle large volume of data through keyboard. It takes a lot of time to enter the entire data. If the user makes a mistake while entering the data, he/she has to start from the beginning again. If the same data is to be entered again at some later stage, again we have to enter the same data. These problems invite the concept of data file in which data can be stored on the disks and read whenever necessary, without destroying data. A file is a place on the disk where a group of related data is stored. The data file allows us to store information permanently and to access and alter that information whenever necessary. Programming in C has various library functions for creating and processing data files. Mainly there are two types of data files: 1.Stream Oriented: Stream oriented data files are generally easier to work with and are therefore more commonly used. It can be subdivided into two categories: a. Text Files: A text file is a human readable sequence of characters and the words they form that can be encoded into computer readable formats such as ASCII. A text contains only textual characters with no special formatting such as underlining or displaying characters in boldface or different font. There is no graphical information, sound or video files. A text file known as an ASCII file and can be read by any word processor. Text file stores information in consecutive characters.
www.ckundan.com.np 2 These characters can be interpreted as individual data items or as a component of Strings or numbers. b. Binary Files: In contrast to ASCII files, which contain only characters (plain text), binary files contain additional code information. A binary file is made up of machine readable symbols that represent 1’s and 0’s. The binary file content must be interpreted by a program that understands in advance exactly how it is formatted. These files organize data into blocks containing contagious bytes of information. These blocks represents more complex data structures, such as arrays and structures. 2.System Oriented: System oriented data files are more closely related to the computer’s operating system than stream oriented data files. They are somewhat complicated to work with though their use may be more efficient for certain kinds of application. A separated set of procedures with accompanying library functions is required to process system oriented data files. File Pointer in C: File pointer is a pointer which is used to handle and keep track on the files being accessed. A new data type called “FILE” is used to declare file pointer. This data type is defined in stdio.h file. File pointer is declared as FILE *fp. Where, ‘fp’ is a file pointer. fopen() function is used to open a file that returns a FILE pointer. Once file is opened, file pointer can be used to perform I/O operations on the file. fclose() function is used to close the file. Opening and Closing a Data File: Before a program can write a file or read from a file, the program must open it. Opening a file established a link between the program and the operating system. This provides the operating system, the name of the file and the mode in which the file is to be opened. While working with high level data file, we need buffer area where information is stored temporarily in the course of transferring data between computer memory and data file. The process of establishing a connection between the program and file is called opening file.

www.ckundan.com.np 4 4. Write and Read (“w+”): This mode is same as “w” mode but in this mode we can also read and modify the data. If the file doesn’t exist then a new file is created and if the file exists then previous data is erased. 5. Read and Write (“r+”): This mode is same as “r” mode but in this mode we can also write and modify existing data. The file to be opened must exist and the previous data of file is not erased. Since, we can add new data and modify existing data so this mode is also called update mode. 6. Append and Read (“a+”): This mode is same as “a” mode but in this mode we can also read the data stored in the file. If the file doesn’t exist, a new file is created and if the file already exists then new data is appended at the end of existing data in this mode. The file opening mode is binary files are similar to text mode, which are (rb, rb+, wb, wb+, ab, and ab+). The nature of each mode is similar to the mode of text file. Character “b” is added to each mode to indicate the binary mode. The file that was opened using fopen() function must be closed when no more operations are to be performed on it. After closing file, connection between file and program is broken. Syntax for closing file is: Fclose(ptr_variable): On closing the file, all the buffer associated with it is flushed and can be available for other files. Example: Fclose(fp1); Fclose(fp2); Library Functions for Reading or Writing From or to a File: 1.Unformatted Input/output Functions: a. Character Input/output Functions: fputc(): It is used to write a character to a file. Syntax: fputc(char_variable, file_ptr, variable);

Tài liệu liên quan

x
Báo cáo lỗi download
Nội dung báo cáo



Chất lượng file Download bị lỗi:
Họ tên:
Email:
Bình luận
Trong quá trình tải gặp lỗi, sự cố,.. hoặc có thắc mắc gì vui lòng để lại bình luận dưới đây. Xin cảm ơn.