Tuesday, February 18, 2020

C Language : FILE I/O or File Handling

FILE I/O in C Language or File Handling in C Language:


File Type
Data File
Text File
Stored Records in a Format
Stored Text without any format
Structured File
Un-Structured File
Like MS Access
Like MS Word, Notepad File, etc.


=> We can create a Text or Data File using C programs and can perform many task (operations) on it.
=> We can perform operations on files like Writing data, Adding and Modifying data, Deleting data, Accessing Record/Text, etc.
=>  We have to open a file with a specific accessing mode like read, write or append.

File Opening Mode
Operation
Key
Write
“w”
Read
“r”
Append
“a”


FILE Pointer:- We have to require a platform to open a file to perform an operation, called File Pointer.

FILE:- This is a constant and used to declare a File Pointer to handle a file.
Syntax:
        FILE  *file-pointer;
Example:
        FILE  *FP;

fopen( ) :- This function is used to open a file using a file-pointer with a specific opening mode. Required a header file “stdio.h”.
Syntax:
        file-pointer=fopen(file-name, opening-mode);
Example:
        FP=fopen(“STUD.DAT”, “w”);



fclose( ) :- This function is used to close an opened file after completing the task. Required a header file “stdio.h”.
Syntax:
        fclose(file-pointer);
Example:
        fclose(FP);

~~~~~~@~~~~~~





Write Process (Store Data): INPUT

                    scanf( )                           fprintf( )

Keyboard =========> Memory ===========> File




Read Process (Access Data): OUTPUT

                fscanf( )                                  printf( )
File ===============> Memory =============> Monitor



fprintf( ) :- This function is used to store (write) data from memory to a file. It requires a header file “stdio.h”.
        Syntax:
                fprintf(file-pointer, “format specifier”, variable-name);
        Ex:
                int RN;
char NAME[12];
float PER;

                fprintf(FP,“%d %s %f”, RN, NAME, PER);




fscanf( ) :- This function is used to read data from a file to memory. It requires a header file “stdio.h”.
        Syntax:
                fscanf(file-pointer, “format specifier”, &variable-name);
        Ex:
                int RN;
char NAME[12];
float PER;

                fscanf(FP,“%d %s %f”, &RN, &NAME, &PER);

~~~~~~@~~~~~~


// Store/write following information of 5 students in a data file:      
        Roll Number, Name and Percentage.       S_WRITE.C
#include<stdio.h>
#include<conio.h>
void main( )
{
        int RN, i ;
        char NAME[10];
        float PER;

        FILE  *FP;
        FP=fopen(“STUD.DAT” , “w”);

        clrscr( );

        if(FP==NULL)
        {
                printf(“File can not be created”);
                exit( );
        }

        printf(“Input following information of 5 students… \n”);

        for(i=1; i<=5; i++)
        {
                printf(“Input Roll Number = “);
                scanf(“%d”, &RN);
                printf(“Input Name = “);
                scanf(“%s”, &NAME);
                printf(“Input Percentage = “);
                scanf(“%f”, &PER);
                fprintf(FP, “%d %s %f\n”, RN, NAME, PER);
        }

        fclose(FP);
        printf(“\n Student data file created successfully”);
}

~~~~~~@~~~~~~


// Reading student records from a data file:   
        Roll Number, Name & Percentage.         S_READ1.C
#include<stdio.h>
#include<conio.h>
void main( )
{
        int RN, i ;
        char NAME[10];
        float PER;

        FILE  *FP;
        FP=fopen(“STUD.DAT” , “r”);

        clrscr( );

        if(FP==NULL)
        {
                printf(“File can not be Opened”);
                exit( );
        }

        printf(“ROLL NO.    NAME      PERCENTAGE \n”);

        for(i=1; i<=5; i++)
        {
                fscanf(FP, “%d %s %f\n”, &RN, &NAME, &PER);
                printf(“%d %s %f\n”, RN, NAME, PER);
        }

        fclose(FP);
}

~~~~~~@~~~~~~




Error Handling in FILE I/O:

NULL :- This constant is used to check possibility of making/opening a file.

if(FP==NULL)
{
        printf(“File can not be created or opened”);
}


EOF :- This constant is used to check record pointer for approaching end of the file. (Text File)


feof( ) :- This function is used to check record pointer for approaching end of the file. (Data File)
while(feof(FP)==0)
{
        Accessing/reading Records
}

        OR

while(feof(FP))
{
        Accessing/reading Records
}

~~~~~~@~~~~~~


fputc( ) :- This function is used to write (store) a character to the text file. It requires a header file “conio.h”.
Syntax:
        fputc(Character-Variable , File-Pointer);
Ex:
        char CH;
        fputc(CH, FP);



13 => This is an ASCII code of ENTER Key.
27 => This is an ASCII code of ESC / ESCAPE Key.


// Write a C program to create a text file to store some text. (press ESC key to exit from file)                        T_WRITE.C
#include<stdio.h>
#include<conio.h>
void main()
{
        char CH;
        FILE *FP;
        clrscr();
        FP=fopen("mydoc .txt","w");
        if(FP==NULL)
        {
                printf("Text file cannot be created");
                exit();
        }

        while((CH=getche())!=27)
        {
                if(CH==13)
                {
                        fputc('\n' , FP);
                        printf("\n");
                }
                else
                {
                        fputc(CH, FP);
                }
        }
        fclose(FP);
        printf("\n A text file is created");
}

~~~~~~@~~~~~~



fgetc( ) :- This function is used to READ a character from a text file. It requires a header file “conio.h”.
Syntax:
        char-Variable = fgetc(File-Pointer);
Ex:
        char CH;
        CH = fgetc(FP);



// Write a C program to read text from previously created text file.                                              T_READ.C
#include<stdio.h>
#include<conio.h>
void main()
{
        char CH;
        FILE *FP;
        clrscr();
        FP=fopen("mydoc .txt","r");
        if(FP==NULL)
        {
                printf("File cannot be opened");
                exit();
        }

        while((CH=fgetc(FP))!=EOF)
        {
                printf("%c",CH);
        }
        fclose(FP);
}

~~~~~~@~~~~~~






// Write a C program to copy text from one file to another file.        F_COPY.C
#include<stdio.h>
#include<conio.h>
void main()
{
        char CH;
        FILE *SFP,*DFP;
        clrscr();

        SFP = fopen("mydoc.txt","r");
        if(SFP==NULL)
        {
                printf("Source file can not be opened");
                exit();
        }

        DFP = fopen("yourdoc.txt","w");
        if(DFP==NULL)
        {
                printf("Destination file can not be created");
                fclose(SFP);
                exit();
        }

        while((CH=fgetc(SFP))!=EOF)                 //Copy Process
        {
                fputc(CH, DFP);
        }
        fclose(SFP);
        fclose(DFP);
        printf("\n1 file(s) copied");
}

~~~~~~@~~~~~~

No comments:

Post a Comment