-->

File operation con

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