C and C++ Programming Resources

Custom Search

File Handling in C Language

In this section, we will discuss about files which are very important for storing information permanently. We store information in files for many purposes, like data processing by our programs.

What is a File?

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 text files and binary files. These two classes of files will be discussed in the following sections.

ASCII Text files

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 signalled the intention to process a text file.

Binary files

A binary file is no different to a text file. It is a collection of bytes. In C Programming Language a byte and a character are equivalent. Hence a binary file is also referred to as a character stream, but there are two essential differences.

  1. No special processing of the data occurs and each byte of data is transferred to or from the disk unprocessed.
  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 Programming Language, 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.

Creating a file and output some data

In order to create files we have to learn about File I/O i.e. how to write data into a file and how to read data from a file. We will start this section with an example of writing data to a file. We begin as before with the include statement for stdio.h, then define some variables for use in the example including a rather strange looking new type.

/* Program to create a file and write some data the file */
#include <stdio.h>
#include <stdio.h>
main( )
{
     FILE *fp;
     char stuff[25];
     int index;
     fp = fopen("TENLINES.TXT","w"); /* open for writing */
     strcpy(stuff,"This is an example line.");
     for (index = 1; index <= 10; index++)
     	fprintf(fp,"%s Line number %d\n", stuff, index);
     fclose(fp); /* close the file before ending program */
}

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.

Reading (r)

When an r is used, 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 program that reads a file and display its contents on screen.

/* Program to display the contents of a file on screen */
#include <stdio.h>
void main()
{
   FILE *fopen(), *fp;
   int c;
   fp = fopen("prog.c","r");
   c = getc(fp) ;
   while (c!= EOF)
   {
   		putchar(c);
		c = getc(fp);
   }
   fclose(fp);
}

Writing (w)

When a file is opened for writing, it will be created if it does not already exist and it will be reset if it does, resulting in the deletion of any data already there. Using the w indicates that the file is assumed to be a text file.

Here is the program to create a file and write some data into the file.

#include <stdio.h>
int main()
{
 FILE *fp;
 file = fopen("file.txt","w");
 /*Create a file and add text*/
 fprintf(fp,"%s","This is just an example :)"); /*writes data to the file*/
 fclose(fp); /*done!*/
 return 0;
}

Appending (a)

When a file is opened for appending, it will be created if it does not already exist and it will be initially empty. If it does exist, the data input point will be positioned at the end of the present data so that any new data will be added to any data that already exists in the file. Using the a indicates that the file is assumed to be a text file.

Here is a program that will add text to a file which already exists and there is some text in the file.

#include <stdio.h>
int main()
{
    FILE *fp
    file = fopen("file.txt","a");
    fprintf(fp,"%s","This is just an example :)"); /*append some text*/
    fclose(fp);
    return 0;
}

Outputting to the file

The job of actually outputting to the file is nearly identical to the outputting we have already done to the standard output device. The only real differences are the new function names and the addition of the file pointer as one of the function arguments. In the example program, fprintf replaces our familiar printf function name, and the file pointer defined earlier is the first argument within the parentheses. The remainder of the statement looks like, and in fact is identical to, the printf statement.

Closing a file

To close a file you simply use the function fclose with the file pointer in the parentheses. Actually, in this simple program, it is not necessary to close the file because the system will close all open files before returning to DOS, but it is good programming practice for you to close all files in spite of the fact that they will be closed automatically, because that would act as a reminder to you of what files are open at the end of each program.

You can open a file for writing, close it, and reopen it for reading, then close it, and open it again for appending, etc. Each time you open it, you could use the same file pointer, or you could use a different one. The file pointer is simply a tool that you use to point to a file and you decide what file it will point to. Compile and run this program. When you run it, you will not get any output to the monitor because it doesn’t generate any. After running it, look at your directory for a file named TENLINES.TXT and type it; that is where your output will be. Compare the output with that specified in the program; they should agree! Do not erase the file named TENLINES.TXT yet; we will use it in
some of the other examples in this section.

Reading from a text file

Now for our first program that reads 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.

#include <stdio.h>
   main( )
   {
     FILE *fp;
     char c;
     funny = fopen("TENLINES.TXT", "r");
     if (fp == NULL)
		printf("File doesn't exist\n");
     else {
      do {
       c = getc(fp); /* get one character from the file
       */
         putchar(c); /* display it on the monitor
       */
       } while (c != EOF); /* repeat until EOF (end of file)
     */
     }
    fclose(fp);
   }

In this program we check to see that the file exists, and if it does, we execute the main body of the program. If it doesn’t, we print a message and quit. If the file does not exist, the system will set the pointer equal to NULL which we can test. The main body of the program is one do while loop in which a single character is read from the file and output to the monitor until an EOF (end of file) is detected from the input file. The file is then closed and the program is terminated. At this point, we have the potential for one of the most common and most perplexing problems of programming in C. The variable returned from the getc function is a character, so we can use a char variable for this purpose. There is a problem that could develop here if we happened to use an unsigned char however, because C usually returns a minus one for an EOF – which an unsigned char type variable is not
capable of containing. An unsigned char type variable can only have the values of zero to 255, so it will return a 255 for a minus one in C. This is a very frustrating problem to try to find. The program can never find the EOF and will therefore never terminate the loop. This is easy to prevent: always have a char or int type variable for use in returning an EOF. There is another problem with this program but we will worry about it when we get to the next program and solve it with the one following that.

After you compile and run this program and are satisfied with the results, it would be a good exercise to change the name of TENLINES.TXT and run the program again to see that the NULL test actually works as stated. Be sure to change the name back because we are still not finished with TENLINES.TXT.

Pages: [Page - 1] [Page - 2] [Page - 3] [Page - 4]

Tags: , ,

There are 277 Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.

  • Heart says:

    How do you write and read from a binary file using the C Language?.?

  • sunaina says:

    imagine we have a file that has the following text:
    Two, one, 2,
    One fish, two fish?!?
    red_fish; BLUE FISH!!!

    how can I ignore all non-alphabetic characters and print the alphabetic characters to standard output – each word on a line by itself, with all the letters converted to lower case
    output should be:
    one
    two
    one
    one
    fish
    two
    fish
    red
    fish
    blue
    fish
    Kindly let me know at the earliest and email to sunainabansal2003@yahoo.com
    thank u

  • Tabish says:

    i want to show data which is store in a doc file please help me if any one have code for this.
    tabishahmad1@gmail.com
    please mail me.

  • kanu says:

    bubble sorting using register and timer and restart the sorting again where stop

  • Mathews says:

    How to convert the contents of a file to upper case. file may contain bopth alphabetic and non-alphabetic characters.

  • antonisl says:

    Hi,could you please help me with this:
    “Write program in C which it will accept from the user the name of file of text and stores the characters that are found in the odd places in the file “odd.txt” and those that find in even in “even.txt”.”

  • martz says:

    -create a program that will compute the running time of another program, not showing the running
    time in seconds but by number of (Tfetch, Tstore, Tdelete, etc.) or any related running time axioms..

    COULD SOMEONE HELP ME WITH THIS PROBLEM????…
    hoping for your kind response…
    just send to my email: canon_martina@yahoo.com.ph

  • martinnitram says:

    please i need help in two or three days about this c problem; develop a simple user authentication system using C language. The system should allow you to register usernames into a file and then check if the user is valid. Remember that to check the validity of the user, you must first enter the username of that user and the system checks if this user exists in the file that stores the users. If a wrong username is supplied to the system, it should display to you an appropriate message, for example “Invalid User”. When you are through with authentication, the system should display all the users stored in the file. you can kindly post your reply to aine.brian@yahoo.com

  • vikibe says:

    Hi, i want to delete a line in a file and replace it with another line.can i use the file pointer or help me?

  • supriya says:

    hi…can anyone help me in writing a c program to delete a particular word from file thnks in advance

  • vinnu says:

    hi…..all
    write a program i c without using main().how posssibbe? if anybody has know pls send me answer with code on my email address.vinay.intech@rediffmail.com this question
    is a infosys software system

    • kitty says:

      we can do by using macros
      #define a main
      void a()
      {
      printf(“hello “);
      }
      if u run this program it will execute
      open a new file and include this file so that there is no word main
      and it will show the output
      like
      #include “xx.c”
      save the above program with xx.c

  • chinmayee says:

    hi can anyone help me with this problem does anyone know how password is hidden and typed as a dot using file handling plz do reply at cbaitharu@yahoo.co.in

  • umesh314 says:

    have any kind of problem or want any source code —
    contact
    umesh314@gmail.com
    or umesh314@yahoo.com

  • RayJay1990 says:

    Hey I was wondering if you could help me.

    If I had a list of information about several cars in a text file, e.g. Color, Year, Horsepower etc. and I needed to make a program that will read the info and store it in an array of structures, what would the code be like?

    Also I was wondering, how would the text file look like too. Is it simply just your basic text file with no computation done to it what so ever, like just simply separated by spaces, e.g. (Blue 1999 160) with the list going downwards or do you have add stuff to it e.g fscan etc.

    I’m just starting off programing and this point has been confusing me for a very long time, any help would be deeply appreciated. Thanks

  • [...] display data with first line and label it 1 and then incremented by 1 for each additional line. http://www.mycplus.com/tutorials/c-p…file-handling/ It is always better to search Google before posting [...]

  • patel asif says:

    hi
    plz help me
    write in file
    add record
    modify record
    delete record
    display record
    send program
    use the following structur
    struct stud {int stud no,char*name,char*courser};

  • harendra says:

    hi,plz help me to write a programme in c that if a txt file having 13 colums and we want to read only mid 4 colums and want to write in new .dat file only those 4 colums which we need so what is the programme for that..plz reply me on my mail id harendra_dadhich@rediffmail.com.

  • ayu says:

    hi. this is my first file handling program. it does not run even when it compiles withouit any errors. plz suggest the correction.

    #include
    #include
    void main()
    {
    FILE *fp;
    char ch;
    fp=fopen(“D:\olddrive\study\TurboC3\BIN\luc\chap12\ayu.txt”,”r”);
    while(1)
    {
    ch=fgetc(fp);
    if(ch==EOF)
    break;
    printf(“%c”,ch);
    }
    fclose(fp);
    getch();
    }

  • binta dalwadi says:

    i have a big trouble my intelligent friends…
    i have a program of “Find and Replace using File Handling” concept..
    In that i am neither able to find a string from a created file nor replace a new string in place of given string…

    Please try 2 help me…
    I m in very big trouble..
    you can catch me on my email id.. it is “bd_love_20@yahoo.com”

    It will b so nice of you if you could try to help me..
    I m available on my yahoo id…

    Thanx..
    Binta Dalwadi

  • Tanishq says:

    hii… can any one help me out for source code for:
    actually i hv to write a program that creates a text file and counts the number of lines that start with ‘A’??
    please send the source for the obove que as early as possible,,
    on my id tanishqagarwal2011@gmail.com
    thanks in advance!!

  • bharath reddy says:

    How to open an excel file in c ???

  • sumanthboss says:

    HI all,
    Im using ubuntu . i want to take a file as an input , and the file contains a query which is to be exected on mysql server using C language strictly . pls help me . i’ve tried . im able to read the contains of the file , but cannot execute it .

    text file name is test.txt and it contains SELECT *from table; and this should be executed

  • pallavi says:

    hai friends i have a doubt on files concept,write a program on student data and display on moniitor,the student data is student roll no
    name
    6 subject marks,tot,avg,res,grade
    the output is in the form of
    no name s1 s2 s3 s4 s5 s6 tot avg res grd
    ———————————————
    1 raj 65 76 88 99 100 56

  • vijaynitj says:

    //THIS PROGRAM WRITE RECORDS IN A FILE AND THEN READ IT RECORDS IN SORTED ORDER BY NAMES IN RECORD
    //AND DISPLAY THEM ON SCREEN
    #include
    #include
    #include
    int main()
    {
    FILE *fw,*fr;
    char opsn=’y';
    char *p;
    int count=0;
    //for writing records into file
    struct student
    {
    char name[20];
    int age;
    }s;

    fw=fopen(“op.txt”,”w”);
    while(opsn==’y')
    {
    count++;
    printf(“Enter student name and age\n”);
    scanf(“%s%d”,s.name,&s.age);
    fprintf(fw,”%3s%3d\n”,s.name,s.age);
    printf(“Wanna enter another record(y|n)\n”);
    fflush(stdin);
    opsn=getche();
    }
    fclose(fw);
    //for reading records rom file
    fr=fopen(“op.txt”,”r”);
    struct st
    {
    char nm[20];
    int ag;
    }s2[count];

    printf(“\nRecord from file is\n”);
    int i=0,j;
    char temp[20];
    while(fscanf(fr,”%s%d”,s.name,&s.age)!=EOF)
    {
    strcpy(s2[i].nm,s.name);
    s2[i].ag=s.age;
    i++;
    }
    // for sorting records
    for(i=0;i<count;i++)
    { for(j=i+1;j0)
    {
    strcpy(temp,s2[i].nm);
    strcpy(s2[i].nm,s2[j].nm);
    strcpy(s2[j].nm,temp);
    }}}
    //for displaying records
    for(i=0;i<count;i++)
    printf("\n%s\t\t%d",s2[i].nm,s2[i].ag);
    fclose(fr);
    }

  • shivam says:

    hey guys i need a favor..
    how to give a text and background color in file handling..
    i have then the commands but its not working..

  • Ramendra says:

    There are two files containing some information like File 1 got data of trains and days it works on and second one got its route. I need to make a c program which reads data from both file in order to get a train schedule for a particular city on particular day.How to do it ? Create hypothetical data of yours and please explain.

  • #include<stdio.h>
    
    main()
    {
    
    		FILE *sp, *tp;
    		char ch,nextch;
    		int index = 0;
    
    		sp = fopen("d:\hello1.txt","r");
    		tp = fopen("d:\remove.txt","w");
    
    		if(sp == NULL)
    		{
    			perror("Cannot Open Source File");
    			return 0;
    		}
    
    		while( (ch = fgetc(sp)) != EOF)
    		{
    				if(ch == 10)
    				{
    					index++;
    					nextch = fgetc(sp);
    
    					if(ch == 10 && nextch != 10)
    					{
    						fputc(ch, tp);
    						fputc(nextch, tp);
    					}
    
    					
    				}
    
    				if(ch != 10)
    				{
    					fputc(ch,tp);
    					index++;
    				}
    
    		}
    
    }
  • aman says:

    Hey see there are three more modes to open a file that are w+ : use to open a file which can be both read and write
    r+ : use to open a file in which we can write more with also doing alteration in before written data
    a+ : this is used to read and write data but wihtout doing alteration in existing data in file if file already exists
    for creating numeric data files we have to open them in binary mode like if we want to read an existing file we will use mode that is “rb”
    eg: fp=fopen(“abc”,”rb”);
    same for write mode

  • jai says:

    you better give the format of the file. otherwise it will be difficult
    ——– Original Message ——–

    ——– Original Message ——–
    hello. please send to me a code to sort the contents of file…..thank alot

  • ashley says:

    i want to do a database using file handling in cpp.its a music shoppe database..plz help

  • bhaskerbobby says:

    Thanks Krishna
    your article helped me a lot


Leave a Reply

You must be logged in to post a comment.