Add

Top 5 Usefull Program for File Handling And Streams in C Language | file handling in c Language

Let's see the 5  Usefull  Program for File Handling And Streams in C Language.

Best Way To Learn C Programming free

Basic For Loop program in c language

Top 10 Basic C Programs For Beginners

1.Program to Print names of all Files present in a Directory.

  • dirent.h header file contains variables and functions related to directory streams.

#include<stdio.h>

#include<dirent.h>

int main(void)

{

    DIR *d;

    struct dirent *dir;

    d = opendir(".");

    if (d)

    {

        while ((dir = readdir(d)) != NULL)

        {

            printf("%s\n", dir->d_name);

        }

        closedir(d);

    }

    return(0);

}

2.Program to find the Size of any File.

We will be using fseek() and ftell() functions to find the size of the file. There are others ways to find the file size as well, like looping on the whole content of file and finding out the size, but the File Handling functions makes it a lot easier.

#include<stdio.h>

#include<conio.h>

void main()

{

    FILE *fp;

    char ch;

    int size = 0;

   fp = fopen("MyFile.txt", "r");

    if (fp == NULL)

    {

        printf("\nFile unable to open...");

    }

    else

    {

        printf("\nFile opened...");

    }

    fseek(fp, 0, 2);    /* File pointer at the end of file */

    size = ftell(fp);   /* Take a position of file pointer in size variable */

    printf("The size of given file is: %d\n", size);

    fclose(fp);

}

3.Program to create a File & write Data in it.

  •  Program to create a new file and then storing information in it.

#include<stdio.h>

#include<conio.h>

void main()

{

    FILE *fptr;

    char name[20];

    int age;

    float salary;

    /* open for writing */

    fptr = fopen("emp.txt", "w");

    if (fptr == NULL)

    {

        printf("File does not exist.\n");

        return;

    }

    printf("Enter the name:\n");

    scanf("%s", name);

    fprintf(fptr, "Name  = %s\n", name);

    printf("Enter the age:\n");

    scanf("%d", &age);

    fprintf(fptr, "Age  = %d\n", age);

    printf("Enter the salary:\n");

    scanf("%f", &salary);

    fprintf(fptr, "Salary  = %.2f\n", salary);

    fclose(fptr);

}

4.Program to reverse the content of a File.

#include<stdio.h>

#include<errno.h>

/*

    to count the total number of characters

    inside the source file

*/

long count_characters(FILE *);

void main()

{

    int i;

    long cnt;

    char ch, ch1;

    FILE *fp1, *fp2;

    if (fp1 = fopen("File_1.txt", "r"))

    {

        printf("The FILE has been opened...\n");

        fp2 = fopen("File_2.txt", "w");

        cnt = count_characters(fp1);

        /*

            Make the pointer fp1 to point at the

            last character of the file

        */

        fseek(fp1, -1L, 2);

        printf("Number of characters to be copied %d\n", ftell(fp1));

        while (cnt)

        {

            ch = fgetc(fp1);

            fputc(ch, fp2);

            fseek(fp1, -2L, 1); // shifts the pointer to the previous character

            cnt--;

        }

        printf("\n**File copied successfully in reverse order**\n");

    }

    else

    {

        perror("Error occured\n");

    }

    fclose(fp1);

    fclose(fp2);

}

/*

    Count the total number of characters in the file

    that *f points to

*/

long count_characters(FILE *f)

{

    fseek(f, -1L, 2);

    /*

        returns the position of the

        last element of the file

    */

    long last_pos = ftell(f);

    last_pos++;

    return last_pos;

}

5.Program to copy content of one File into another File.

We already know how to open a file, read contents of a file and write into a file. So in this program, we will read from one file and simultaneously write into the other file, till we reach end of first file.

#include<stdio.h>

#include<stdio.h>

void main()

{

    /*

        File_1.txt is the file with content and,

        File_2.txt is the file in which content of File_1

        will be copied.

    */

    FILE *fp1, *fp2;

    char ch;

    int pos;

    if ((fp1 = fopen("File_1.txt", "r")) == NULL)

    {

        printf("\nFile cannot be opened.");

        return;

    }

    else

    {

        printf("\nFile opened for copy...\n ");

    }

    fp2 = fopen("File_2.txt", "w");

    fseek(fp1, 0L, SEEK_END);   // File pointer at end of file

    pos = ftell(fp1);

    fseek(fp1, 0L, SEEK_SET);   // File pointer set at start

    while (pos--)

    {

        ch = fgetc(fp1);    // Copying file character by character

        fputc(ch, fp2);

    }

    fcloseall();

}

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.