Introduction to C File

What is file?  What is the use?

A file is collection of letters, numbers and special character. It may be a program, a database, a dissertation, a reading list on computer disk.

Sometimes we want to store output or input of some program for future reference, but once a program terminated we lost entire data. These can be easily store by simply redirecting to a file.

The use of file is to keep and store large number of data. We can categorize file I/O functions into.

  1. Text File I/O functions
  2. Binary File I/O functions

Next in this chapter we will learn how to handle C file i/o and o/p.

There are four operations can be done to a file.

1. Opening a new file.

We use fopen() function to create or open a new file, given below prototype.

FILE *fopen(const char *filename, const char *mode);

A simple program to open a new file.

0 #include<stdio.h>

1 main()

2 {

3            FILE *fp;

4            fp = fopen(“gyantoaday.txt”, “w+”);

5            if (NULL == fp)

6            {

7                        Printf (“Error: Not able to open new file\n”);

8                        exit (1);

9            }

10            fprintf(fp, “Welcome to gyantoday. \n”);

11            fclose(fp);

12 }

Explanation:

When above program compiled and executed it create a new file with name gyantoday.txt in current directory and writes Welcome to gyantoday.

At line no. 3 – fp is a pointer of type FILE. This is used to store memory location of file, So communication between file and program can be done.

At line no. 4 – fopen is used to create a new file of name gyantoday.com and return a FILE pointer to fp. If file is not open then return NULL value. The second argument w+ tells mode of a file.

At line no. 10 – fprintf is used to write ‘Welcome to gyantoday’ to the newly created file.

At Line no. 11 – flcose is used to close the file.

Given below file modes and their description.

r Opens an existing text file for reading purpose.
w Opens a text file for writing, if it does not exist then a new file is created. Here your program will start writing content from the beginning of the file.
a Opens a text file for writing in appending mode, if it does not exist then a new file is created. Here your program will start appending content in the existing file content.
r+ Opens a text file for reading and writing both.
w+ Opens a text file for reading and writing both. It first truncate the file to zero length if it exists otherwise create the file if it does not exist.
a+ Opens a text file for reading and writing both. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.

2. Opening an existing file.

Now I hope you can write a program to open an existing file. Simply change the file mode in above program to “r” but remember File mode “r” is used to open an existing file for reading purpose only. So we can not write anything by using fprintf() or fputc() functions.

A simple program to open an existing file for reading purpose.

#include<stdio.h>

main()

{

            FILE *fp;

            fp = fopen(“gyantoaday.txt”, “r”);

            if (NULL == fp)

            {

                        Printf (“Error: Not able to open new file\n”);

                        exit (1);

            }

 fclose(fp);

}

3. Reading from and writing to a file. 

Above operations can be done using standard file  library functions. In next chapter we will learn how and where to use these functions.

4. Closing a file.

As we have seen in first example, flcose is used to close the file.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.