File System in C Programming: Working with Files

Rumman Ansari   Software Engineer   2024-07-05 04:07:09   9454  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Often it is not enough to just display the data on the screen. This is because if the data is large, only a limited amount of it can be stored in memory and only a limited amount of it can be displayed on the screen. It would be inappropriate to store this data in memory for one more reason. Memory (RAM) is volatile and its contents would be lost once the program is terminated. So if we need the same data again it would have to be either entered through the keyboard again or would have to be regenerated programmatically. Obviously, both these operations would be tedious. At such times it becomes necessary to store the data in a manner that can be later retrieved and displayed either in part or in whole. This medium is usually a file on the disk. This chapter discusses how file I/O operations can be performed.

Before beginning our discussion of the C file system it is necessary to know the difference between the terms streams and files.

The C I/O system provides a consistent interface to the developer independent of the actual device being accessed. That is, the C I/O system provides a level of abstraction between the programmer and the device. This abstraction is called a stream, and the actual device is called a file. It is important to understand how streams and files interact.

Stream

There are mainly two types of streams: text and binary. Streams are largely device independent, the same function that can write to a disk file can also write to another type of device, such as the console. The C file system is designed to work with a wide variety of devices, including terminals, disk drives, and tape drives. Even though each device is very different, the buffered file system transforms each into a logical device called a stream. All streams behave similarly.

stream view

standard streams are preconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin), standard output (stdout) and standard error (stderr). Originally I/O happened via a physically connected system console (input via keyboard, output via monitor), but standard streams abstract this. When a command is executed via an interactive shell, the streams are typically connected to the text terminal on which the shell is running but can be changed with redirection, e.g. via a pipeline.

Text Streams

Text stream is a sequence of characters. Standard C states that a text stream is organized into lines terminated by a newline character.

Binary Streams

binary stream is a sequence of bytes that has a one-to-one correspondence to the bytes in the external device—that is, no character translations occur. Also, the number of bytes written (or read) is the same as the number on the external device.

Files

Before we start doing file input/output let us first find out how data is organized on the disk. A file represents a sequence of bytes, regardless of it being a text file or a binary file. C programming language provides access to high-level functions as well as low level (OS level) calls to handle file on your storage devices. This chapter will take you through the important calls for file management.

Data Organization

All data stored on the disk is in binary form. How this binary data is stored on the disk varies from one OS to another. However, this does not affect the C programmer since he has to use only the library functions written for the particular OS to be able to perform input/output. It is the compiler vendor's responsibility to correctly implement these library functions by taking the help of OS.

Not all files have the same capabilities. For example, a disk file can support random access, while some printers cannot. This brings up an important point about the C I/O system: All streams are the same, but all files are not.

why file system in important

Real life situations involve a large volume of data and in such cases, the console oriented I/O operations pose two major problems
  • It becomes cumbersome and time-consuming to handle large volumes of data through terminals.
  • The entire data is lost when either the program is terminated or computer is turned off, therefore, it is necessary to have the more flexible approach where data can be stored on the disks and read whenever necessary, without destroying the data. This method employs the concept of files to store data.

File Operations

There are different operations that can be carried out on a file. These are:
  • Creation of a new file
  • Opening an existing file
  • Reading from a file
  • Writing to a file
  • Moving to a specific location in a file (seeking)
  • Closing a file

File operation functions in C

Function Name

Operation

fopen()

Creates a new file for use
Opens a new existing file for use

fclose

Closes a file which has been opened for use

getc()

Reads a character from a file

putc()

Writes a character to a file

fprintf()

Writes a set of data values to a file

fscanf()

Reads a set of data values from a file

getw()

Reads an integer from a file

putw()

Writes an integer to the file

fseek()

Sets the position to a desired point in the file

ftell()

Gives the current position in the file

rewind()

Sets the position to the beginning of the file

MCQ Available

There are 1 MCQs available for this topic.

1 MCQ