C File Input Output

File is not the new concept. File is a more flexible approach than the previous data entry approach. Files are the mega data structure in information processing. Storage of information and its retrieval are the vital components of system design and information system. By using files data can be stored on the disks and can be read whenever you want without destroying the date. A file is place on the disk where a group of related data is stored permanently. By using files we can sense the data. Files establish a permanent link between inputs and ouputs as data can be stored and retrieved. A computer programmber or data entry operator always prefers to enter data in the files instead of storing the data temporarily in the main memory using the direct keyboard facility.

Definition

A file is a collection of related data structures pertaining to a single entity. A file having payroll data processes the information of pay elements and not about the personal and educational information of an employee. So we can say file has mainly two basic elements: information and a single entity.

Persistent programs, those that retain data even after they are closed, must write their data to a file. Upon reopening, the file must then be read to restore variables and settings. File operations refer to the input and output of data to external files.

Types of Files

There are two main types of Files used in C Programming. These are below:

Files Operations

The processing of files has number of operation according to the user’s requirement and the problem definition. But some of the commonly used file operations are as follows:

  • Naming a file
  • Opening a file
  • Reading data from a file
  • Writing data to a file or creation of data file
  • Closing a file
  • Updating a file

Following are the most important file management functions available in C:

function purpose
fopen () Creating a file or opening an existing file
fclose () Closing a file
fprintf () Writing a block of data to a file
fscanf () Reading a block data from a file
getc () Reads a single character from a file
putc () Writes a single character to a file
getw () Reads an integer from a file
putw () Writing an integer to a file
fseek () Sets the position of a file pointer to aspecified location
ftell () Returns the current position of a file pointer
rewind () Sets the file pointer at the beginning of a file

How to Create a File

Whenever you want to work with a file, the first step is to create a file. A file is nothing but space in a memory where data is stored. To create a file in a ‘C’ program following syntax is used,

FILE *fp; 
fp = fopen ("file_name", "mode"); 

In the above syntax, the FILE is a data structure which is defined in the standard library. fopen is a standard function which is used to open a file.

  • If the file is not present on the system, then it is created and then opened.
  • If a file is already present on the system, then it is directly opened using this function.

fp is a file pointer which points to the type file.

Naming a File

File name (data file name) should not be more than eight characters and three characters for extension. File name can be defined and enclosed by using the fopen() high-level I/O function. The general Syntax is as follows:

fopen("filename","mode");

For example, “Student.dat” is a data file name, which is defined and enclosed in the fopen() function as:

fopen("Student.dat","mode");

Opening a File

A file is opened by using fopen() in build high-leve input/ouput function. The general Syntax is as follows:

FILE *fp;
fp = fopen("filename","mode");

Closing a File

A file is Close by using fclose() in build high-leve input/ouput function. The general Syntax is as follows:

fclose(file-pointer);

For example:

FILE *fl;
fl = fopen("Student","w");
fclose(fl);

Input/Ouput statements used in file handling

File have different Input/Ouput statements used for different purpose. These are used to put data from variables to data file. The various Input/Ouput functions used with standard I/O are discussed as below:

  • Character Input/Ouput (Standard I/O)
  • String Input/Ouput (Standard I/O)
  • Formatted Input/Ouput (Standard I/O)
  • Record (Block) Input/Ouput (Standard I/O)

A Program to Write data into File and Read data from File

The program is as follows:

#include<stdio.h>
#include<conio.h>

void main()
{
  FILE *p;
  char ch;

  printf("Enter the Lines");
  p = fopen("Student.txt","w");

  while((ch=getchar()!=EOF)
  {
    putc(ch,p);
  }
  fclose(p);

  printf("After the Inputting");

  p = fopen("Student.txt","r");

  while((ch=getc(p))!EOF)
  {
    printf("%c",ch);
  }
  fclose(p);
  getch();
}

Write a Program to find How many Words are there in a File

The program is as follows:

#include<stdio.h>
#include<conio.h>
void main()
{
  FILE *p;
  char ch;
  int len = 1;
  clrscr();
  printf("Enter the Lines");
  p = fopen("Student.txt","w");

  while((ch=getchar()!=EOF)
  {
    putc(ch,p);
  }
  fclose(p);

  printf("After the Inputting");

  p = fopen("Student.txt","r");

  while((ch=getc(p))!EOF)
  {
    if(ch=="")
    {
      len ++;
    }
  }
  printf("Length of Words : %d",len);   fclose(p);
  getch();
}

Copy One File to another File

The program is as follows:

#include
#include
void main()
{
  FILE *p,*p2;
  char ch;
  clrscr();
  printf("Enter the Lines");
  p = fopen("Student.txt","w");

  while((ch=getchar()!=EOF)
  {
    putc(ch,p);
  }
  fclose(p);

  p = fopen("Student.txt","r");
  p2 = fopen("Teacher.txt","w");

  while((ch=getc(p))!EOF)
  {
    putc(ch,p2)
  }
  fclose(p);
  fclose(p2);
  }
  p = fopen =("Student.txt","r");
  while((ch=getc(p))!EOF)
  {
    printf("%c",ch);
  }
  fclose(p);
  p2 = fopen =("Teacher.txt","r");
  while((ch=getc(p2))!EOF)
  {
    printf("%c",ch)
  }
  fclose(p2);
  getch();
}
Related Post