0% found this document useful (0 votes)
72 views11 pages

Exercise 24 Harshkumar 23

The document contains 8 code examples demonstrating various file handling operations in C programming: 1) Displaying the contents of a file 2) Copying the contents of one file to another 3) Reversing the first n characters in a file 4) Merging the contents of two sorted files into a third file 5) Merging lines alternately from two files into a new file 6) Counting the number of characters in a file 7) Comparing two files and returning whether they are equal 8) Reading a file and displaying its contents with line numbers

Uploaded by

Harsh kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views11 pages

Exercise 24 Harshkumar 23

The document contains 8 code examples demonstrating various file handling operations in C programming: 1) Displaying the contents of a file 2) Copying the contents of one file to another 3) Reversing the first n characters in a file 4) Merging the contents of two sorted files into a third file 5) Merging lines alternately from two files into a new file 6) Counting the number of characters in a file 7) Comparing two files and returning whether they are equal 8) Reading a file and displaying its contents with line numbers

Uploaded by

Harsh kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Exercise 24

1) Write a C program to display the contents of a file.


PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE * fPtr;
    char ch;
 
    fPtr = fopen("data/file1.txt", "r");
 
    if(fPtr == NULL)
    {
        printf("Unable to open file.\n");
        printf("Please check whether file exists and you have read privilege.\
n");
        exit(EXIT_FAILURE);
    }
 
    printf("File opened successfully. Reading file contents character by
character. \n\n");
 
    do
    {
        ch = fgetc(fPtr);
 
        putchar(ch);
 
    } while(ch != EOF);
    fclose(fPtr);
    return 0;
}

Output:
Unable to open file.
Please check whether file exists and you have read privilege.
2) Write a C program to copy the contents of one file to
another
PROGRAM:
#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fptr1, *fptr2;
    char filename[100], c;

    printf("Enter the filename to open for reading \n");


    scanf("%s", filename);

    fptr1 = fopen(filename, "r");


    if (fptr1 == NULL)
    {
        printf("Cannot open file %s \n", filename);
        exit(0);
    }

    printf("Enter the filename to open for writing \n");


    scanf("%s", filename);

    fptr2 = fopen(filename, "w");


    if (fptr2 == NULL)
    {
        printf("Cannot open file %s \n", filename);
        exit(0);
    }

    c = fgetc(fptr1);
    while (c != EOF)
    {
        fputc(c, fptr2);
        c = fgetc(fptr1);
    }

    printf("\nContents copied to %s", filename);

    fclose(fptr1);
    fclose(fptr2);
    return 0;
}

Output:
Enter the filename to open for reading
Harsh
Cannot open file Harsh
___________________________________________________________________

3) Write a C program to reverse the first n characters in a file, where n


is given by the user.
PROGRAM:
#include <stdio.h>
#include <string.h>
#define MAX 100

void reverseContent(char *x)


{

    FILE *fp = fopen(x, "a+");

    if (fp == NULL)
    {
        printf("Unable to open file\n");
        return;
    }

    char buf[100];
    int a[MAX], s = 0, c = 0, l;

    fprintf(fp, " \n");


    rewind(fp);

    while (!feof(fp))
    {
        fgets(buf, sizeof(buf), fp);
        l = strlen(buf);
        a = s += l;
    }

    rewind(fp);
    c -= 1;

    while (c >= 0)
    {
        fseek(fp, a, 0);
        fgets(buf, sizeof(buf), fp);
        printf("%s", buf);
        c--;
    }

    return;
}

int main()
{
    char x[] = "newfile.txt";

    reverseContent(x);
    return 0;
}

Output:
My
name is
HARSH KUMAR

HARSH KUMAR
name is
My
4) Two files DATA1 and DATA2 contain sorted lists of integers. Write a C program
to merge the contents of two files into a third file DATA i.e., the contents of the
first file followed by those of the second are put in the third file.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
 
int main()
{FILE *fp1 = fopen("file1.txt", "r");
FILE *fp2 = fopen("file2.txt", "r");
FILE *fp3 = fopen("file3.txt", "w");
char c;
 
if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
{
puts("Could not open files");
exit(0);
}
while ((c = fgetc(fp1)) != EOF)
fputc(c, fp3);
 while ((c = fgetc(fp2)) != EOF)
fputc(c, fp3);
 
printf("Merged DATA1.txt and DATA2.txt into DATA.txt");
 
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}

Output:
Merged DATA1.txt and DATA2.txt into DATA.txt

5) Write a program that merges lines alternately from two files and writes the
results to new file. If one file has less number of lines than the other, the
remaining lines from the larger file should be simply copied into the target file.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE *f1, *f2, *fp;
    char ch1 = 'a', ch2 = 'a';
    f1 = fopen("File (e)1.txt", "r");
    f2 = fopen("File (e)2.txt", "r");
    fp = fopen("File (e)3.txt", "w");

    if (f1 == NULL)
    {
        printf("Can't open the file1\n");
        exit(1);
    }
    if (f2 == NULL)
    {
        printf("Can't open the file1\n");
        exit(2);
    }

    puts("\nWork on progress\n.\n.\n.\n.\n");

    while (1)
    {
        if (ch1 != EOF)
        {
            ch1 = fgetc(f1);
            /*A line is ends when a . is encounter*/
            while (ch1 != '.')
            {
                if (ch1 == EOF)
                    break;
                fputc(ch1, fp);
                ch1 = fgetc(f1);
            }
            if (ch1 != EOF)
                fputc('.', fp);
        }
        if (ch2 != EOF)
        {
            ch2 = fgetc(f2);
            while (ch2 != '.')
            {
                if (ch2 == EOF)
                    break;
                fputc(ch2, fp);
                ch2 = fgetc(f2);
            }
            if (ch2 != EOF)
                fputc('.', fp);
        }
        if (ch1 == EOF && ch2 == EOF)
            break;
    }
    printf("\nTask completed.\nExiting . . . \n");

    return 0;
}

Output: Can't open the file1

6) C Program to count number of characters in the file

PROGRAM:
#include <stdio.h>
void main()
{
    char ch;
    int count = 0;
    FILE *fptr;
    fptr = fopen("text.txt", "w");
    if (fptr == NULL)
    {
        printf("File can't be created\a");
        exit(0);
    }
    printf("Enter some text and press enter key:\n");
    while ((ch = getche()) != '\r')
    {
        fputc(ch, fptr);
    }
    fclose(fptr);
    fptr = fopen("text.txt", "r");
    printf("\nContents of the File is:");
    while ((ch = fgetc(fptr)) != EOF)
    {
        count++;
        printf("%c", ch);
    }
    fclose(fptr);
    printf("\nThe number of characters present in file is: %d", count);
}

Output: File cant be created


7) Write a program that compares two files and return 0 if they are equal and 1 if
they are not.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>

int compareFile(FILE *fPtr1, FILE *fPtr2, int *line, int *col);

int main()
{
    FILE *fPtr1;
    FILE *fPtr2;
    char path1[100];
    char path2[100];

    int diff;
    int line, col;

    printf("Enter path of first file: ");


    scanf("%s", path1);
    printf("Enter path of second file: ");
    scanf("%s", path2);

    fPtr1 = fopen(path1, "r");


    fPtr2 = fopen(path2, "r");

    if (fPtr1 == NULL || fPtr2 == NULL)


    {
        printf("\nUnable to open file.\n");
        printf("Please check whether file exists and you have read privilege.\
n");
        exit(EXIT_FAILURE);
    }

    diff = compareFile(fPtr1, fPtr2, &line, &col);

    if (diff == 0)
    {
        printf("\nBoth files are equal.");
    }
    else
    {
        printf("\nFiles are not equal.\n");
        printf("Line: %d, col: %d\n", line, col);
    }

    fclose(fPtr1);
    fclose(fPtr2);

    return 0;
}
int compareFile(FILE *fPtr1, FILE *fPtr2, int *line, int *col)
{
    char ch1, ch2;

    *line = 1;
    *col = 0;

    do
    {
        ch1 = fgetc(fPtr1);
        ch2 = fgetc(fPtr2);

        if (ch1 == '\n')
        {
            *line += 1;
            *col = 0;
        }

        if (ch1 != ch2)
            return -1;

        *col += 1;
    } while (ch1 != EOF && ch2 != EOF);

    if (ch1 == EOF && ch2 == EOF)


        return 0;
    else
        return -1;
}

Output: Enter path of first file: desktop\new1.txt


Enter path of second file: desktop\new2.txt

Files are not equal.


Line: 3, col: 18
8) Write a program to read a file and display contents with its line numbers.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fPtr;

    char ch;

    fPtr = fopen("desktop/new1.txt", "r");

    if (fPtr == NULL)
    {
        printf("Unable to open file.\n");
        printf("Please check whether file exists and you have read privilege.\
n");
        exit(EXIT_FAILURE);
    }

    printf("File opened successfully. Reading file contents character by


character. \n\n");
    do
    {
        ch = fgetc(fPtr);

        putchar(ch);

    } while (ch != EOF);


    fclose(fPtr);

    return 0;
}

Output:
File opened successfully. Reading file contents character by character.

___________________________________________________________________
___________________________________________________________________

You might also like