


What is a File in C?

Abstractly, a file is a collection of bytes stored on a secondary storage device, which is generally a disk of some kind. The collection of bytes may be interpreted, for example, as characters, words, lines, paragraphs and pages from a textual document; fields and records belonging to a database; or pixels from a graphical image. The meaning attached to a particular file is determined entirely by the data structures and operations used by a program to process the file. It is conceivable (and it sometimes happens) that a graphics file will be read and displayed by a program designed to process textual data. The result is that no meaningful output occurs (probably) and this is to be expected. A file is simply a machine decipherable storage media where programs and data are stored for machine usage.

Essentially there are two kinds of files that programmers deal with i.e. text files and binary files. These two classes of files will be discussed in the following sections.

1. What is ASCII Text file in C Programming?

A text file can be a stream of characters that a computer can process sequentially. It is not only processed sequentially but only in forward direction. For this reason a text file is usually opened for only one kind of operation (reading, writing, or appending) at any given time. Similarly, since text files only process characters, they can only read or write data one character at a time. (In C Programming Language, Functions are provided that deal with lines of text, but these still essentially process data one character at a time.) A text stream in C is a special kind of file. Depending on the requirements of the operating system, newline characters may be converted to or from carriage-return/linefeed combinations depending on whether data is being written to, or read from, the file. Other character conversions may also occur to satisfy the storage requirements of the operating system. These translations occur transparently and they occur because the programmer has signaled the intention to process a text file.

2. What is a Binary file in C Programming?


  2. C Programming Language places no constructs on the file, and it may be read from, or written to, in any manner chosen by the programmer.

Binary files can be either processed sequentially or, depending on the needs of the application, they can be processed using random access techniques. In C, processing a file using random access techniques involves moving the current file position to an appropriate place in the file before reading or writing data. This indicates a second characteristic of binary files. They a generally processed using read and write operations simultaneously. For example, a database file will be created and processed as a binary file. A record update operation will involve locating the appropriate record, reading the record into memory, modifying it in some way, and finally writing the record back to disk at its appropriate location in the file. These kinds of operations are common to many binary files, but are rarely found in applications that process text files.

How files are manipulated in C?

Now let’s see how files are created and processed in C programming language. In C, fopen()  function is used to get a pointer to file which can be used by compiler to input and output operations on files. The standard form or fopen()  function is:

A file can be opened in different modes depending upon the requirements. The fopen()  function uses the following modes to open the file.

  1. r – open for reading

How to create a file in C?


The type FILE is used for a file variable and is defined in the stdio.h file. It is used to define a file pointer for use in file operations. Before we can write to a file, we must open it. What this really means is that we must tell the system that we want to write to a file and what the file name is. We do this with the fopen()  function illustrated in the first line of the program. The file pointer, fp in our case, points to the file and two arguments are required in the parentheses, the file name first, followed by the file type. The file name is any valid DOS file name, and can be expressed in upper or lower case letters, or even mixed if you so desire. It is enclosed in double quotes. For this example we have chosen the name TENLINES.TXT. This file should not exist on your disk at this time. If you have a file with this name, you should change its name or move it because when we execute this program, its contents will be erased. If you don’t have a file by this name, that is good because we will create one and put some data into it. You are permitted to include a directory with the file name.The directory must, of course, be a valid directory otherwise an error will occur. Also, because of the way C handles literal strings, the directory separation character must be written twice. For example, if the file is to be stored in the PROJECTS sub directory then the file name should be entered as “\PROJECTS\TENLINES.TXT”. The second parameter is the file attribute and can be any of three letters, r, w, or a, and must be lower case.

How to read a file in C Programming?

When an 'r'  is used as parameter to fopen() , the file is opened for reading, a 'w'  is used to indicate a file to be used for writing, and an 'a'  indicates that you desire to append additional data to the data already in an existing file. Most C compilers have other file attributes available; check your Reference Manual for details. Using the 'r'  indicates that the file is assumed to be a text file. Opening a file for reading requires that the file already exist. If it does not exist, the file pointer will be set to NULL and can be checked by the program. Here is a small C program that reads a file and display its contents on screen.

How to writing to a file in C?


How to Append data to a file in C?



Closing a file in C


How to read text from a file using C?

Now for our first program that reads text from a file. This program begins with the familiar include, some data definitions, and the file opening statement which should require no explanation except for the fact that an 'r'  is used here because we want to read it.


How to read and write Binary Files in C?

There are two main functions used to read and  write binary files in C Programming i.e.  fread()  and fwrite() . Both of these functions uses pointer to a memory location which can be array. Because they accept pointers, you can also use these functions with any other data structures as well. Standard declaration of fread()  and fwrite() is as below:


How to read contents of Binary File in C?

Related Article: File Handling in C++