Wednesday, November 16, 2022

file handling in c programming notes exam program

 Topics

      • File handling in c
      • Create a file in c
      • Opening a file in c
      • Reading a file in c
      • Moving to a specific location in a file in c
      • Closing file in c 

File Handling in c

So far we have gone through C program output is not stored anywhere. But there may be some situation where we need to store the work done so that we can use this stored data at a later time as and when required. C provides us the facility to store the data in a file using the concept of file handling / Data file handling . Different operations that can be performed on a file are:
  1.  Creation of a new file (fopen with attributes as "a","a+",w" or "w+"
  2. Opening an existing file (fopen)
  3. Reading from file (fscanf or fgets)
  4. Writing to a file (fprint or fputs)
  5. Moving to a specific location in a file (fseek, rewind)
  6. closing a file (fclose)

Fopen()

This function is used to open a file to perform operations such as reading, writing etc. In a C program, we declare a file pointer and use the function fopen() to open a file for performing various operations,
        For Example:
                               FILE *fp;
                               fp=fopen("filename","mode");
Here,
        fp -- is the file pointer tothe datatype FILE.
        filename-- is the name of the file with full path.
        mode-- mode in which file will be opened

Opening or Creating file in c

For opening a file , we use fopen() function with the required access modes. Some of the commonly used file access modes are mentioned below.

File opening modes in c

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+             Opens a text file in read and write mode.
 w+           Opens a text file in read and write mode.
 a+            Opens a text file in read and write mode.
 rb             Opens a binary file read mode.
 wb            Opens a binary file write mode.
 ab             Opens a binary file append mode.
 rb+           Opens a binary file read and write mode
 wb+         Opens a binary file read and write mode
 ab+          Opens a binary file read and write mode

Closing a File (fclose function) in c

  • The C library function fclose(FILE *fp) close the file. If we don't close use fclose() it may lead to loss of data.
  • All buffers are flushed.
  • int fclose(FILE *fp)
  • This method return zero it the stream is successfully closed. On failure , EOF is retured.

    •     fprintf()
    •     fputs()

fputc() function in c 

fputc()  - The fputs() function is used to write a single character into file.
        For exam 
         #include<stdio.h>
         manin()
         {     FILE *fp;
            fp=fopen("file.txt","w"); //opening file
            fputs("a",fp); //writing single character into file
            fclose(fp); //closing file
            }

fprintf() function in c

fprintf()  - The function is used to write string into a file pointer by the file pointer.
      For exam
     {
        FILE *fp;
        fp=fopen("file.txt","w");
        fprinf(fp,"File healding in c programming");
        fclose()
        return o;

fputs function in c 

fputs()   -  The fputs() function is used to writes a line of characters into the file.
        For exam
        {
            FILE *fp;
            fp=fopen("file.txt","w");
            fputs(fp , "File heading in c programming");
            getch();
         }
 

No comments: