Topics

6/recent/ticker-posts

File Handling In C language (Part 1)

 File Handling in c language


File : It is a physical memory space on the Hard Disk. It contains data in some format ( text format / binary format ) . Files are used to store the data permanently.

 

File operations.

1)     Opening a File.

2)     Reading data from the file

3)     Writing data to the file

4)     Closing a file.




File Management functions:


<stdio.h> header file provides below functions to manage the files

1.     fopen()  à to open already existing file for read or write or append and also to create new file

2.     fclose()  à to close the file which is already opened for read or write or append

3.     fgetc()  and getc() à to read character from the file

4.     fputc()  and putc() à to write a character to the file

5.     fgetw() and getw() à to read an integer from the file

6.     fputw() and putw() à to write an integer to the file

7.     fscanf()  à to read formatted data from the file like scanf() from keyboard

8.     fprintf() à to write formatted data to the file

9.     sscanf() à to read formatted data from string buffer

10.  spritnf() à to write formatted data to string buffer

11.  fread() à to read structured data from the file

12.  fwrite() à to write structured data to the file

13.  fseek() à to place the file pointer at required position in a file ( random access )

14.  rewind() à to place the file pointer at the beginning of a file

15.  ftell() à returns current position of file pointer in a file

16.  remove() à to delete a file

17.  rename ()  à to rename a file

 

Working with fopen(): 

 

  FILE * fopen ( char   *file_path,  char * mode);

 

                       FILE à It is shortcut  name given to the file structure from <stdio.h>

                       FILE *  à It is a return type of fopen() function. it is pointer to file buffer.

 

fopen() function creates a temporary file buffer in the ram memory and transfers data from hard disk to ram file buffer and return address of that buffer.

 

On successfully opening a file fopen() returns a pointer to file ( address )

If fail to open a file it return NULL pointer

 

 

NULL is a pre-defined constant from <stdio.h>

 

#define NULL 0

 

Standard file pointers:

 

1.     FILE  *stdin à to refer to input buffer

2.     FILE *stdout à to refer to output buffer

3.     FILE *stderr à to refer to error buffer

 

                       file_path à It is a location of the file

                       Eg:

                        “C:\\project\\test\\file1”  à absolute path of the file

 

                       “./file1” à current working directory

 

fopen() à default format opening file is text format

 

mode:  It is a purpose of opening a file.

These are of three types.

1.     Read mode

2.     Write mode

3.     Append mode

 

Read mode :

                       à text file

                       à binary file

 

                       “r”  (or)  “rt”  à read only text file

                       “rb” à read only binary file

                      

                       “r+”  (or)  “rt+”  à to open  text file read mode and also to support write as well as

                                                append

                       “rb+”  à to open binary file in read mode and also to support write and append.

Eg:

                       FILE *fp;

 

                       fp = fopen(“c:\\techchannelwithdsk\\test\\file1.txt”,”r”);

 

                       if file is already exist then fopen() opens the file and place the file pointer at the beginning of the file and returns address.

 

                       if file does not exist then  fopen() returns null pointer ( NULL )

 

                       if(fp==NULL)

                       {

                            printf(“\n file not found”);

                       }

 

NULL is pre-defined constant from <stdio.h> header file.

 

#define NULL 0

 

 

Write mode :

“w”  (or)  “wt”  à write only text file

“wb” à write only binary file

“w+”  (or)  “wt+”  à to open  text file in write mode and also to support read as well as append

“wb+”  à to open binary file in write mode and also to support read and append.

 

In write mode if file already exist then fopen() over writes the file data.

If file does not exist then fopen() creates new file.

 

 

Append mode :

                       “a”  (or)  “at”  à append only text file

                       “ab” à append only binary file

                       “a+”  (or)  “at+”  à to open  text file append mode and also to support read as well as write

                       “ab+”  à to open binary file in append mode and also to support read and write.

à In append mode if file already exist then fopen() opens the file place the file pointer at the end of the file.

à If file does not exist then fopen() creates new file.

 

Suggested coding :

                       When expecting to open existing file, use read mode

                        When expecting to create a new file, use write mode


Post a Comment

0 Comments