-->

File operation

File operation
 C provides a number of functions that helps to perform basic file operations. 
     Following are the functions,    
Function
description
getc()
reads a character from a file
putc()
writes a character to a file
fscanf()
reads a set of data from a file
fprintf()
writes a set of data to a file
getw()
reads a integer from a file
putw()
writes a integer to a file
fseek()
set the position to desire point
ftell()
gives current position in the file
rewind()
set the position to the beginning point
  

The fputs() function

The fputs() function is used to write string(array of characters) to the file.

Syntax of fputs() function

              fputs(char str[], FILE *fp);
The fputs() function takes two arguments, first is the string to be written to the file and second is the file pointer where the string will be written.

The fgets() function

The fgets() function is used to read string(array of characters) from the file.

Syntax of fgets() function

              fgets(char str[],int n,FILE *fp);
The fgets() function takes three arguments, first is the string read from the file, second is size of string(character array) and third is the file pointer from where the string will be read.
The fgets() function will return NULL value when it reads EOF(end-of-file).

  Example
#include<stdio.h>  
#include<conio.h>       
void main()
{  
FILE *fp,*fp1;      
clrscr(); 
fp=fopen("std.txt",w");
fputs("hello c programming",fp);  
  
fclose(fp);  fp1=fopen("std.text","r");
printf("%s",fgets(text,200,fp)); 
fclose(fp1);
getch();  
}  

C fseek() function

The fseek() function is used to set the file pointer to the specified offset. It is used to write data into file at desired location.
Syntax:
Fseek(file pointer,offset,whence)

There are 3 constants used in the fseek() function for whence: SEEK_SET, SEEK_CUR and SEEK_END.

fputc()

It is used to write one by one character in file. Function prototype is:

fputc(int character, filepointer );

character: The character to be written in the file.
File pointer which hold the address of file in that data will be written.

fgetc()



It is used to read one by one character from file. Function prototype is:
int fgetc ( file pointer );
File Pointer to the file from which data will be read.

The fprintf ( ) :  The fprintf statement is used to write data items to the file whose file pointer is opened in the writing mode. ‘fprintf’ perform I/O operations an files the  
 Syntax
fprintf (fp, "control string", list);
 Where ‘fp’ is a file pointer associated with a file that has been opened for writing.
The control string are the content to write in the file .
Variable list: more than one variable used
Ex Write to a text file using fprintf()
Void main( )
{ int age=18;
      Char name[ ]=”BKEC”;
      FILE *fp1;        
      *fp1=fopen(“stud.txt”,”w”);      
  fprintf (fp1, "%s %d”, name ,age); }

The fscanf ( ): The  fscanf function is used to read data items to the file whose pointer is opened in the reading mode. fscanf function performs I/O operations on files.
Syntax
fscanf (fp, “control string”, list);
Where ‘fp’ is a file pointer associated with a file that has been opened for reading
 The control string are the content to read from the file & stored.
Ex Read from a text file using fscanf()
#include <stdio.h>
 main()
{
   int age;
   char name[10];
   FILE *fp;
   fptr = fopen("stud.txt","r")

   fscanf(fptr,"%s %d",&name,&age);

   printf("Value of n=%d", num);
   fclose(fptr);

}


     



            

No comments:

Post a Comment

Basic structure of c program