INPUT/OUTPUT OPERATIONS

Once a file is opened, reading out of or writing to it is accomplished using the standard I/O routines.
There are 2 distinct ways to perform file operations.

1. low level i/o functions: It is used in UNIX system calls

2. High level i/o functions: It uses functions in c standard i/o library.

fopen () : It creates a new file for use or opens an existing file for use.

fclose (): It closes a file which has been opened for use.

getw (): This function returns the integer value from a given file and increment the file pointer position to the next message.

Syntax: getw (fptr);

Where fptr is a file pointer which takes the integer value from file.

putw (): This function is used for writing an integer value to a given file.

Syntax: putw (value,fptr);

Where fptr is a file pointer Value is an integer value which is written to a given file.

Example Program for getw() and putw()

Write a program to read integer data from the user and write it into the file using putw() and read the same integer data from the file using getw() and display it on the output screen.

#include<stdio.h>
#include<conio.h>
void main()
{
   FILE *fp;
   int n;
   clrscr();
		fp=fopen("c.dat", "wb+");
	    printf("Enter the integer data");
	    scanf("%d",&n);
	    while(n!=0)
	    {
	       putw(n,fp);
	       scanf("%d",&n);
	    }
	    rewind(fp);     
	    printf("Reading data from file");
	  
	    while((n=getw(fp))!=EOF)
	      {
	            printf("%d\n",n);
	      }
	    fclose(fp);
    getch();
}

OUTPUT:

Enter the integer data
10
20
34
45
0
Reading data from file
10
20
34
45
0

fgetc ()

This function is same as the getc () function. It also reads a single character from a given file and increment the filepointer.It returns EOF,if the end of the file is reached or it encounters an error.

Syntax: fgetc (fptr);
Ch=fgetc (fptr);
Where fptr is a file pointer.
Ch is a variable which receive the character returned by fgetc ().

fputc()

This function writes the character to the specified stream at the current file position and then increments the file position indicator.
Syntax: fputc(ch,fptr);
Where fptr is a file pointer
Ch is a variable written to the file which is pointed by file pointer.
Example program for fgetc() and fputc():

Write a program to read character by character from the user and write them into a file using fputc() and read the same data from the file using fgetc() and display that on the screen.

#include<stdio.h>
#include<conio.h>
void main()
{
     FILE *fp;
     Char ch;
     Clrscr();
     fp=fopen("a.txt", "w+");
     printf("Enter data into file\n");
     while((ch=getchar())!=EOF)
     {
         fputc(ch,fp);
     }
     printf("Reading data from file\n");
     rewind(fp);   // Here rewind()  will move file pointer to the beginning of the file
     while((ch=fgetc(fp))!=EOF)
     {
           printf("%c",ch);
     }
     fclose(fp);
    getch();
}

OUTPUT:
Enter data into file
Hai hello how r u I am fine
Reading data from a file
Hai hello how r u I am fine

fgets()

This function is used to read a string from a given file and copies the string to a memory location which is referenced by an array.
Syntax: fgets(sptr,max,fptr);
Where sptr is a string pointer, which points to an array.
max is the length of the array.
fptr is a file pointer ,which points to a given file.
This function read max-1 characters and places them into array which is pointed by sptr.This function read character until either a newline or an end of the file or size of the array occurs. It appends a null character (‘\0’) at the end of the string. It returns a null pointer if either an end of file or an error encountered.

fputs ()

This function is used to write a string to a given file.
Syntax: fputs (sptr, fptr);
Where sptr is a pointer which points to an array
fptr is a file pointer which is pointed to a given file.

Example program for fgets() and fputs()
Write a program to read multiple number of strings from the user and write them to a file using fputs() and read the same strings from file using fgets() and display on the output screen.

#include<stdio.h>
#include<conio.h>
void main()
{
     FILE *fp;
     Char st[80];
     Clrscr();
     fp=fopen("a.txt", "w+");
	     printf("Enter data into file (type end to stop)\n");
	     gets(st);
	     while(strcmp(st, "end")!=0)
	     {
	        fputs(st,fp);
	        fputs("\n",fp);
	        gets(st);
	      }
	     rewind(fp);
	     printf("Reading data from file");
	     fgets(st,80,fp);
	     
	 	 while(feof(fp)==0)  //feof()  will check whether end of file has been reached or   not.
	     {
	         puts(st);
	        fgets(st,80,fp);
	      }
  	fclose(fp);
  getch();
}


OUTPUT:

  Enter data into file (type end to stop):
Hello
Hai
How r u
end
Reading data from file
Hello
Hai
How r u
end