You are on page 1of 11

Exercise 25

1) C Program to Delete a specific Line from a Text File


PROGRAM:
#include <stdio.h>
int main()
{

    FILE *fptr1, *fptr2;


    char file1[] = "file1.txt";
    char file2[] = "file2.txt";
    char curr;
    int del, line_number = 0;

    printf("Please enter the line number you want to delete : ");


    scanf("%d", &del);
    fptr1 = fopen(new1, "r");
    fptr2 = fopen(new2, "w");

    curr = getc(fptr1);
    if (curr != EOF)
    {
        line_number = 1;
    }
    while (1)
    {
        if (del != line_number)
            putc(curr, fptr2);
        curr = getc(fptr1);
        if (curr == '\n')
            line_number++;
        if (curr == EOF)
            break;
    }
    fclose(fptr1);
    fclose(fptr2);
}

Output:
Please enter the line number you want to delete : 2
2) C Program to Append the Content of File at the end of Another

PROGRAM: #include <stdio.h>

#include <stdlib.h>

int main()
{
    FILE *fsring1, *fsring2, *ftemp;
    char ch, new1[20], new2[20], new3[20];

    printf("Enter name of first file ");


    gets(new1);
    printf("Enter name of second file ");
    gets(new2);
    printf("Enter name to store merged file ");
    gets(new3);
    fsring1 = fopen(new1, "r");
    fsring2 = fopen(new2, "r");
    if (fsring1 == NULL || fsring2 == NULL)
    {
        perror("Error has occured");
        printf("Press any key to exit...\n");
        exit(EXIT_FAILURE);
    }
    ftemp = fopen(new3, "w");
    if (ftemp == NULL)
    {
        perror("Error has occures");
        printf("Press any key to exit...\n");
        exit(EXIT_FAILURE);
    }
    while ((ch = fgetc(fsring1)) != EOF)
        fputc(ch, ftemp);
    while ((ch = fgetc(fsring2)) != EOF)
        fputc(ch, ftemp);
    printf("Two files merged  %s successfully.\n", file3);
    fclose(fsring1);
    fclose(fsring2);
    fclose(ftemp);
    return 0;
}

Output:
Enter name of first file new1.txt
Enter name of second file new2.txt
Enter name to store merged file merge.txt
Two files merged new3.txt successfully.

3) Program to Capitalize First Letter of every Word in a File.

PROGRAM:

#include <stdio.h>
#include <string.h>

void writeData(char *str)


{
    FILE *fp;

    fp = fopen("includehelp.txt", "w");
    if (fp == NULL)
        return;

    fwrite(str, 1, strlen(str), fp);


    fclose(fp);
}

void readData(char *str)


{
    FILE *fp;

    char ch = 0;
    int cnt = 0;

    fp = fopen("includehelp.txt", "r");
    if (fp == NULL)
        return;

    ch = fgetc(fp);
    while (ch != EOF)
    {
        str[cnt++] = ch;
        ch = fgetc(fp);
    }
    str[cnt] = 0;
    fclose(fp);
}

int main()
{
    char wrtStr[32] = "this is india";
    char readStr[32];

    int flg = 1;
    int cnt = 0;

    writeData(wrtStr);
    readData(readStr);

    while (readStr[cnt] != 0)
    {
        if (flg == 1 && readStr[cnt] != 0x20)
        {
            readStr[cnt] = readStr[cnt] - 32;
            flg = 0;
        }

        if (readStr[cnt] == 0x20 && flg == 0)


            flg = 1;

        cnt++;
    }

    writeData(readStr);

    memset(readStr, 0x00, sizeof(readStr));


    readData(readStr);

    printf("Original data from file: %s\n", wrtStr);


    printf("Update data from file: %s\n", readStr);

    return 0;
}

Output:
Original data from file: i am Harsh Kumar
Update data from file: I Am Harsh Kumar

4) C Program to Convert the Content of File to LowerCase


PROGRAM:

#include <stdio.h>
int main()
{
    FILE *fpoint1, *fpoint2;
    char fchar1, fchar2;
    char name1[20], name2[20];
    printf("\n Give the Existing file name to Read:");
    gets(name1);
    printf("\n Give the file name for the lower case of existing file
contents :");
    gets(name2);
    fpoint1 = fopen(name1, "r");
    fpoint2 = fopen(name2, "w");
    if (fpoint1 == NULL)
        printf("\n The file %s has no content !", name1);
    else
    {
        do
        {
            fchar1 = getc(fpoint1);
            fchar2 = tolower(fchar1);
            fputc(fchar2, fpoint2);
        } while (fchar1 != EOF);
        fcloseall();
    }
    return (0);
}

Output:
Give the Existing file name to Read:new1
Give the file name for the lower case of existing file contents :new2
The file new1 has no content !
5) C Program to Join Lines of Two given Files and Store them in a New
file.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>

int joinfiles(FILE *, FILE *, FILE *);

char ch;

int flag;

void main(int argc, char *argv[])

    FILE *file1, *file2, *target;

    file1 = fopen(argv[1], "r");

    if (file1 == NULL)

    {

        perror("Error Occured!");
    }

    file2 = fopen(argv[2], "r");

    if (file2 == NULL)

    {

        perror("Error Occured!");
    }

    target = fopen(argv[3], "a");

    if (target == NULL)

    {

        perror("Error Occured!");
    }
    joinfiles(file1, file2, target); /* Calling Function */

    if (flag == 1)

    {

        printf("The files have been successfully concatenated\n");


    }
}

int joinfiles(FILE *file1, FILE *file2, FILE *target)

    while ((fgetc(file1) != EOF) || (fgetc(file2) != EOF))

    {

        fseek(file1, -1, 1);

        while ((ch = fgetc(file1)) != '\n')

        {

            if (ch == EOF)

            {

                break;
            }

            else

            {

                fputc(ch, target);
            }
        }

        while ((ch = fgetc(file2)) != '\n')

        {

            if (ch == EOF)
            {

                break;
            }

            else

            {

                fputc(ch, target);
            }
        }

        fputc('\n', target);
    }

    fclose(file1);

    fclose(file2);

    fclose(target);

    return flag = 1;
}

Ouput:
Error Occured!: Bad address
Error Occured!: No such file or directory
Error Occured!: No such file or directory

6). C program to remove empty lines from a 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:
Enter file path: desktop/new.txt
File contents before removing all empty lines.
I am Aditya Mishra

I am learning C programming
Programming with files is fun.
Learning C programming is interesting

7) C program to input numbers from file and write even, * odd and
prime numbers to separate file.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>

int isEven(const int NUM);


int isPrime(const int NUM);

int main()
{
    FILE *fPtrIn,
        *fPtrEven,
        *fPtrOdd,
        *fPtrPrime;

    int num, success;

    fPtrIn = fopen("desktop/numbers.txt", "r");


    fPtrEven = fopen("desktop/even.txt", "w");
    fPtrOdd = fopen("desktop/odd.txt", "w");
    fPtrPrime = fopen("desktop/prime.txt", "w");

    if (fPtrIn == NULL || fPtrEven == NULL || fPtrOdd == NULL || fPtrPrime ==


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

    printf("File opened successfully. Reading integers from file. \n\n");

    while (fscanf(fPtrIn, "%d", &num) != -1)


    {
        if (isPrime(num))
            fprintf(fPtrPrime, "%d\n", num);
        else if (isEven(num))
            fprintf(fPtrEven, "%d\n", num);
        else
            fprintf(fPtrOdd, "%d\n", num);
    }

    fclose(fPtrIn);
    fclose(fPtrEven);
    fclose(fPtrOdd);
    fclose(fPtrPrime);

    printf("Data written to files successfully.");

    return 0;
}

int isEven(const int NUM)


{
    return !(NUM & 1);
}

int isPrime(const int NUM)


{
    int i;
    if (NUM < 0)
        return 0;

    for (i = 2; i <= NUM / 2; i++)


    {

        if (NUM % i == 0)
        {
            return 0;
        }
    }

    return 1;
}

Output:
File opened successfully. Reading integers from file.

Data written to files successfully.

You might also like