January 23, 2013

Files Input/Output in C-language


practical significance of files input and output in C programming language

A file is a place on the disk where the group of related data are stored. The first function is to be performed when we are accessing the files is to open a file. Opening a file establishes a link between the program and the operating system about which file we want to access for what purpose. We provide the operating system to which file we want to access and for what purpose. For accessing a file we just have to name the file and plan whether we want o read data from the file or write data onto the file. The syntax for opening is:

FILE *fp;
inbuilt structure
fp= fopen( “file name”, “mode”);



FILE OPENING MODES

1. r (reading mode):  It opens the file for reading only. The pointer is set to the start of the file.
2. w (writing mode):  It opens the file for writing only. If the file already exists, the contents of that file will be deleted and the pointer will be set to the beginning of the file.
3. a (append mode):  It open the file for append data i.e. adding data in case of an already existing file. The pointer will be set to the end of the file.

Here, FILE is a structure which is defined in the header file stdio.h using fopen function and request the operating system to open a file whose name is given. The fopen function returns a pointer of type FILE. 
Each file that we open will have its own file structure. The file structure contains information about the file being used such as current size of the file, current location in computer memory etc. It also contains a character pointer which points to the character i.e. about to be read. The function fopen will:
1. It searches on the disk for the file to be opened.
2. If the file is present it loads the file from disk to computer memory. If the file isn’t present fopen function will return the NULL value.
3. It setup a character pointer which points to the first character of the file.

Values of variable and data should be given to the program as usual by using input statements like scanf and once the data is available with the program then it should be transferred to the file by using fprintf function.
fprintf function can type all the variables in the file. It means the whole record will be written into the file and format is:

fprintf(fp, “spcd1,spcd2”, var1,var2);
spcd= special characters like &.



DIFFERENT OPERATIONS USED FOR FILE OPENING ARE

1. fopen():  it is used to open a file for reading, writing or appending.
2. putc():  It is used to write a single character onto file.
3. putw():  It is used to write an integer value onto the file.
4. fprintf():  It is used to write any type of data onto the file.
5. getc():  It is user to read single character from the file.
6. getw():  It is used to read any integer value from the file.
7. fscanf():  To read any type of data from the file.
8. fclose():  It is used to close a particular file after it has been processed.
9. feof():  This function tells if the file pointer has reached at the end of the file or not. If it has reached at the end of the file then this function will return a true value otherwise this function returns false value.

This function is frequently used when file is opened in reading mode. Therefore, it normally used with if statements or while loop or do-while loops.




RANDOM ACCESS TO FILES

ftell()  

This function takes a file pointer as argument and returns the number of type long int that corresponds to the current position of the file pointer. This function is useful when we want to save current position of the file pointer. The syntax:
long int n;
n= ftell (fp);


rewind() 

The function rewind takes a file pointer as argument and resets the position of the file pointer to the start of the file.

fseek()

This function can move the file pointer in forward or backward direction by given number of bytes, in forward direction from the beginning of the file or in backward direction from the end of the file. This function is a function of random files. Its main purpose is to skip the file pointer by given number of bytes from the current cursor position of the file. Format:
fseek( fp, number of bytes, cursor position)

Here number of bytes is basically an offset and to move in forward direction ‘+n’ is used i.e. +50 will move the file pointer in forward direction by 50bytes.
Similarly ‘-n’ will move the file pointer backward by n bytes.
File position can be mentioned by using digits:

0 for the beginning of the file
1 for the current position of the file
2 for the end of the file

Example:
fseek (f,5,0)
It says move 5 positions ahead of the beginning.


/*Program to enter height and base of triangle and find its area and save it in file*/

#include<stdio.h>
#include<conio.h>
int main()
{
     FILE *fp;
     float bs,ht,ar=0;
     fp=fopen("tridata","w");
     printf("\n enter base and height of the triangle");
     scanf("%f%f",&bs,&ht);
     fprintf(fp,"Base: %f \nHeight: %f",bs,ht);
     ar=(bs*ht*0.5);
     fprintf(fp,"\nArea is %f",ar);
     fclose(fp);
     printf("\n area of the triangle is %f",ar);
     getch();
     }
output of program to show area of triangle and print its details in a file using C language
output of program to show area of triangle and print its details in a file using C language


/*Program to enter bill details and calculate its total amount and save it in a file*/

#include<stdio.h>
#include<conio.h>
void main()
{
     FILE *fp;
     int bno,i;
     float bamt,d,tamt=0,namt=0;
     fp=fopen("Total amount","w");
     printf("\n enter bill number and bill amt");
     scanf("%d%f",&bno,&bamt);
     fprintf(fp,"Bill No: %d \nBill Amt: %f",bno,bamt);
     tamt=bamt;
     d=tamt*10.0/100.0;
     namt=tamt-d;
     fprintf(fp,"\nNet Amount: %f",namt);
     printf("\n net amount collected is %f",namt);
     fclose(fp);
     getch();
     }
output of program to print bill number and details and save it in a file in C language
output of program to print bill number and details and save it in a file in C language


No comments:

Post a Comment