28-12-2012, 03:11 PM
C FILE INPUT/OUTPUT
1C FILE INPUTOUTPUT.pdf (Size: 1.13 MB / Downloads: 311)
Introduction
- This Module actually shows you how to use the functions readily available in the C standard library. Always remember this, using the standard library (ISO/IEC C, Single Unix specification or glibc); you must know which functions to call and which header files provide these functions. Then, you must be familiar with the proper prototype of the function call.
- The problems normally exist when dealing with the parameters passed to the functions and the return value of the functions. We will explore some of the very nice and one of the heavily used functions that available in the stdio.h header file, for our file processing and management tasks.
- Keep in mind that in C++ we will use member functions in class objects for file processing and some of the advanced file processing examples will be discussed in C++ file I/O Module.
- Storage of data file as you have learned is temporary, all such data is lost when a program terminates. That is why we have to save files on primary or secondary storage such as disks for future usage.
- Besides that we also need to process data from external files that may be, located in secondary storage as well as writing file during software installation and communicating with computer devices such as floppy, hard disk, networking etc.
- And in socket programming (networking) you will also deal a lot with these open, close, read write activities.
- File used for permanent retention of large amounts of data, stored online or offline in secondary storage devices such as hard disk, CD-Rs/DVDs, tape backup or Network Attached Storage (NAS).
Basic of The Data Hierarchy
- Ultimately, all data items processed by a computer are just combinations of zeroes and ones.
- The smallest data item in computer can assume the value 0 or 1, called a bit (binary digit).
- But, human being prefer to work with data in the form of decimal digits (i.e. 0, 1, 2, 3, 4, 5, 6, 7…9), letters (i.e. A – Z and a – z) and special symbols (i.e. $, @, %, &, *, (,), -, +, ? and many others) or in readable format.
- As you know, digits, letters and special symbols are referred to as characters, the keys on your keyboard based on whether the ASCII, EBCDIC, Unicode or other proprietary characters set.
- Every character in a computer’s character set is represented as a pattern of 1’s and 0’s, called byte (consists 8 bits-ASCII, EBCDIC), and for Unicode it uses multibyte or wide characters.
- Characters are composed of bits, and then fields (columns) are composed of characters.
- A field is a group of characters that conveys meaning such as a field representing a month of year.
Files And Streams
- In C, a file can refer to a disk file, a terminal, a printer, a tape drive, sockets or other related devices. Hence, a file represents a concrete device with which you want to exchange information.
- Before you perform any communication to a file, you have to open the file. Then you need to close the opened file after finish exchanging or processing information with it.
- The main file processing tasks may involve the opening, reading, writing and closing.
- The data flow of the transmission from your program to a file, or vice versa, is called a stream, which is a series of bytes. Different with file, a stream is device-independent. All streams have the same behavior including that used in sockets programming such as the Transmission Control Protocol (TCP) and User Datagram Protocol (UDP) streams.
File Name Resolution
- A file name consists of file name components separated by slash (/) characters. On the systems that the GNU C library supports, multiple successive / characters are equivalent to a single / character.
- The process of determining what file a file name refers to is called file name resolution. This is performed by examining the components that make up a file name in left-to-right order, and locating each successive component in the directory, named by the previous component.
- Each of the files that are referenced as directories must actually exist, be directories instead of regular files, and have the appropriate permissions to be accessible by the process; otherwise the file name resolution fails.
- Unlike some other operating systems such as Windows, the Linux system doesn't have any built-in support for file types (or extensions) or file versions as part of its file name prototype.
- Many programs and utilities use conventions for file names. For example, files containing C source code usually have names suffixed with .c and executable files have .exe extension, but there is nothing in the Linux file system itself that enforces this kind of convention.
- May be you can better differentiate those file types by using the –F option for ls directory listing command (ls –F).