-->





File opening modes and file pointer c tutorial

This C programming tutorial explains following concepts
  1. Open a c file and what are the opening modes
  2. What is a File pointer
  3. Close a c file
  4. Read and write character to a file




How to open a file using c language? Also discuss its different file opening modes.
File Opening:
  • Before read or write operation, a file should be opened.
  • All standard file handing function of C are declared in stdio. h header file
  • When a file is opened a point of memory is reserved for file to read and write this memory is called memory buffer.
  • When a file is read it is first stored in the memory buffer and then program reads the file from the memory buffer.
  • When a file is written, then it is written in buffer, then its contents are transferred on the disk.
  • The transfer of data from buffer to the disk is called flushing of buffer.
  • The writing and reading through file through disk is time consuming, therefore time of reading and writing data to a file is reduced through buffer.

The File Pointer:
  • A pointer is like a variable whose content is the address of another memory cell (variable).
  • Represents a pointer to the variable with which it is used.
  • A file is not directly accessible in C.
  • File are accessed through I\O buffers.
  • A file pointer is a variable of FILE type defined in stdio.h
  • FILE* represents a pointer to a variable of FILE type.
  • The fopen( )function is used to open a file.
 Prototype is: FILE*fopen(const char*filename,const char*mode);
 OR
 FILE*fp; // it specifies the file pointer.
 Fp=fopen(file,mode);



File opening Modes:

  • “r” open text file for reading,each file must exists before reading.
  • “w” open a text file for writing, if the file already exists then its contents are overwritten, if it does not exists, it will be created.
  • “a” open a text file for append. Data is appended at the end of existing file. If the file does not exist, it will be created.
  • “r+” open a file for both reading and writing. The file must already exists
  • “w+” open a file for reading and writing, its contents are overwritten if the file does not exist, it is created.
  • “a+” open a text file for both reading and appending. If the file does not exist, it is created for both reading and writing.


The fopen function returns the null pointer, if it fails to open the file (most common reason is file does not exist).

How to close a file in the c language?
File Closing:

  • An open must be close after completing the task.
  • When a file is close then the buffer is flushed (data is transferred on the disk).
  • If the file is not close than the data may be lost.
  • The fclose () function is used to close a file.
  • Syntax:
fclose(fp);
Where fp is a file pointer associated to a file which is to be closed.
fclose () function returns 0 if the close operation is successfully completed.
fclose () function returns EOF if any error occurs.

How to read and write character to a file using C language?

  • Once a file has been opened depending on its opening mode then either a character can be read from a file or character can be written to a file using the following function
  • Putc( ):
  • The putc( ) function writes a single character to the file.
  • The putc ( ) function returns the characters, if it successfully writes it.
  • The putc ( ) function returns the EOF if an error occurs.
Syntax:
putc(character ch , file_pointer);

see example: copy data from one text file to another c source code

Character ch can be character, int, constant or a variable.
File pointer specifies variable of FILE type associated with the file in witch character is to be written.

0 comments:

Post a Comment