-->

Learn to code the on-demand coding Languages in an easy way. We help you understand programming concepts and coding Techniques Better.

Thursday, February 11, 2021

How to Input And Output A File In C | fprintf() and fscanf() in C

 File Input and Output:


    A file is a sequence of bytes, regardless of it being a text file or a binary file. In this tutorial, you will learn how to use C file I/O functions that deal with file handling. File handling in C enables us to create, open, read, write, modify and close the files stored in our local file system.

Functions For File Handling:

Sno

Function

Description

1

fopen()

Opens a new or existing file

2

fclose()

Closes the file

3

fprintf()

Writes data into the file

4

fscanf()

Reads data from the file

5

fputc()

Writes the character into the file

6

fgetc()

Reads the character from the file

7

fseek()

Sets the file pointer to the given position

8

fputw()

Writes an integer to file

9

fgetw()

Reads an integer from a file

10

ftell()

Return the current position

Let's see some file handling functions in detail.

Opening File: fopen() 

    The fopen() function is used to create a new file or open the existing file before read, write, or update. The syntax of fopen() is given below.

    Syntax for fopen():

    FILE *fopen( const char *filename, const char *mode);

    FILE: The call will initialize an object of the type FILE, which contains all the information necessary to control the stream. 

    fopen(): It accepts two parameters.
  • filename(string):  When the file is stored at some location, then we must specify the path at which the file is stored.
         FILE  fopen("C://foldername/filename.txt",  "mode");

   Ex1:FILE  fopen("C://Documents/sample.txt",  "r");

                                    (or)
         Ex2: FILE *f; 
                  f= fopen("C://foldername/filename.txt",  "r");

        We can use Ex1 or Ex2 to open a file.                            
  • mode(string): The mode in which the file to be opened. Based on the mode the further operations are done.
Modes of fopen() Function:
   

Mode

Description

r

Opens a text file in read mode

w

Opens a text file in write mode

a

Opens a text file in append mode

r+

Open a text file in read and write mode

w+

Open a text file in read and write mode

a+

Open a text file in read and write mode

rb

Opens a binary file in read mode

wb

Opens a binary file in write mode

ab

Opens a binary file in append mode

rb+

Opens a binary file in read and write mode

wb+

Opens a binary file in read and write mode

ab+

  • Opens a binary file in read and write mode
  •      
    Closing File: fclose()  

    The fclose() function is used to close the file. The file must be closed after all the operations are done. The syntax of close() is given below.

     Syntax:

      int fclose(FILE *f);

    Declaration of close() function:

      fclose(f);

    Example1: Let's see an example for opening and print the data from the file.

        Program:


       Output:

      

       Explanation: In the above example, I have created a file pointer f. open() function is used to import text data and mode of operation to be performed. a+ mode is to read and write the data from the file. While loop is used to read each character from the file using fgetc function. Don't forget to close the file after all the operations are done.
      
    Example2: Let's see an example for opening, write, and print the data from the file.

        Program:


       Output: 


       Explanation: I have added one more operation to the previous example, that is write operation using fprintf() function. It appends the text to the already existing text in the file. Make sure to close the file after all the operations are done.


    In this tutorial, we have learned some standard I/O functions that deal with opening and closing files in the C. 


    Prev<---typedef in C                                                Next Topic---->Command Line arguments

    No comments:

    Post a Comment