Showing posts with label FileHandling. Show all posts
Showing posts with label FileHandling. Show all posts

How to find if a string is present in a text file?

How to find if a string is present in a text file?

Example:

Input:  file - Java is the programming langualge.
Java is the programming langualge.
Output: Number of occurrences of Java is 2

Approach

Java


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FindingWordInFile {
    public static void main(String[] args) {
        String word = "Java";
        int count = findWord(word);

        if (count > 0) {
            System.out.println("File contains the specified word");
            System.out.println("Number of occurrences is: " + count);
        } else {
            System.out.println("File does not contain the specified word");
        }
    }

    private static int findWord(String word) {
        int count = 0;
        // Reading the contents of the file
        Scanner sc2 = null;
        try {
            sc2 = new Scanner(new FileInputStream("D:\\file.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        while (sc2.hasNextLine()) {
            String line = sc2.nextLine();
            System.out.println(line);
            if (line.indexOf(word) != -1) {
                count = count + 1;
            }
        }
        sc2.close();
        return count;
    }
}


Program to Convert the Content of File to lowercase

Write a program to Convert the Content of the File to lowercase.

Approach 1: Using Temp File

C Program


#include <stdio.h>
#include <ctype.h>

int main()
{

    FILE *fileP, *tmpP;
    char fileName[100];
    char c;

    printf("Enter from file name with full path: ");
    scanf("%s"fileName);

    //open file in read mode
    fileP = fopen(fileName"r");
    if (fileP == NULL)
    {
        printf("Error in opening file \n");
        return -1;
    }

    //open file in write mode
    tmpP = fopen("temp1.txt""w");
    // first file
    c = getc(fileP);
    while (c != EOF)
    {

        putc(tolower(c), tmpP);
        c = getc(fileP);
    }
    fclose(fileP);
    fclose(tmpP);

    //remove from file
    remove(fileName);
    rename("temp1.txt"fileName);
    return 0;
}


Input:

Enter from file name with full path: C:\Users\hello.txt


Input File: hello.txt

THIS IS A C PROGRAM
C IS A PROGRAMMING LANGUAGE
Coding is Fun
Output File: hello.txt

this is a c program c is a programming language coding is fun


Program to Convert the Content of File to UpperCase

Write a Program to Convert the Content of File to UpperCase.

Approach 1: Using Temp File

C Program

#include <stdio.h>
#include <ctype.h>

int main()
{

    FILE *fileP, *tmpP;
    char fileName[100];
    char c;

    printf("Enter from file name with full path: ");
    scanf("%s"fileName);

    //open file in read mode
    fileP = fopen(fileName"r");
    if (fileP == NULL)
    {
        printf("Error in opening file \n");
        return -1;
    }

    //open file in write mode
    tmpP = fopen("temp1.txt""w");
    // first file
    c = getc(fileP);
    while (c != EOF)
    {

        putc(toupper(c), tmpP);
        c = getc(fileP);
    }
    fclose(fileP);
    fclose(tmpP);

    //remove from file
    remove(fileName);
    rename("temp1.txt"fileName);
    return 0;
}

Input:

Enter from file name with full path: C:\Users\hello.txt

Input File: hello.txt


this is a c program c is a programming language Coding is Fun
Output File: hello.txt

THIS IS A C PROGRAM C IS A PROGRAMMING LANGUAGE CODING IS FUN


Program to Find the Size of File using File Handling Function

Write a Program to Find the Size of File using File Handling Function.

C Program
#include <stdio.h>

int main()
{

    FILE *file;
    int size = 0;
    char fileName[100];

    printf("Enter first file name with full path: ");
    scanf("%s"fileName);

    //open file in read mode
    file = fopen(fileName"r");
    if (file == NULL)
    {
        printf("Error in opening file \n");
        return -1;
    }

    //file pointer at the end of file
    fseek(file0LSEEK_END);

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

    printf("The size of given file is : %ld in bytes "size);
    //close the file
    fclose(file);

    return 0;
}
Input:
Enter first file name with full path: C:\Users\merge.txt
Output:
The size of given file is : 44 in bytes 


Merges Lines Alternatively from 2 Files & Print Result

Write a Program that Merges Lines Alternatively from 2 Files & Print Result.

C Program
#include <stdio.h>

int main()
{

    FILE *file1, *file2, *mergeFile;
    char fileName1[1000];
    char fileName2[1000];
    char mergeFileName[1000];
    char c1;
    char c2;

    printf("Enter first file name with full path: ");
    scanf("%s"fileName1);

    printf("Enter second file name with full path: ");
    scanf("%s"fileName2);

    printf("Enter merge file name with full path: ");
    scanf("%s"mergeFileName);

    //open files in read mode
    file1 = fopen(fileName1"r");
    file2 = fopen(fileName2"r");
    if (file1 == NULL || file2 == NULL)
    {
        printf("Error in opening file \n");
        return -1;
    }

    //open file in write mode
    mergeFile = fopen(mergeFileName"w");
    if (mergeFile != NULL)
    {
        printf("File created successfully!\n");
    }
    else
    {
        printf("Failed to create the file.\n");
        return -1;
    }

    char line[1000];

    c1 = fgetc(file1);

    c2 = fgetc(file2);

    int c = 0;
    //iterate till both we reach
    //end of any file
    while (c1 != EOF && c2 != EOF)
    {
        if (c1 != EOF)
        {
            ungetc(c1file1);
            fgets(line1000file1);
            fputs(linemergeFile);
            c1 = fgetc(file1);
        }
        //put new line
        if (c1 == EOF)
        {
            putc('\n'mergeFile);
        }

        if (c2 != EOF)
        {

            ungetc(c2file2);
            fgets(line1000file2);
            fputs(linemergeFile);
            c2 = fgetc(file2);
        }

        //put new line
        if (c2 == EOF)
        {
            putc('\n'mergeFile);
        }
    }
    //if first file remaining
    while (c1 != EOF)
    {
        ungetc(c1file1);
        fgets(line1000file1);
        fputs(linemergeFile);
        c1 = fgetc(file1);
    }
    //if second file remaining
    while (c2 != EOF)
    {
        ungetc(c2file2);
        fgets(line1000file2);
        fputs(linemergeFile);
        c2 = fgetc(file2);
    }
    //close files
    fclose(file1);
    fclose(file2);
    fclose(mergeFile);

    return 0;
}

Input:

Enter first file name with full path: C:\Users\hello.txt
Enter second file name with full path: C:\Users\studentrecord.txt
Enter merge file name with full path: C:\Users\merge.txt

Output:

File created successfully!
Input File: hello.txt

1 2 3 4 5
Input File:studentrecord.txt

Ram Singh Rahul Raju Singh
Output File: merge.txt

1 Ram Singh 2 Rahul 3 Raju 4 Singh 5

Program to Append the Content of File at the end of Another

Write a program to Append the Content of File at the end of Another.

C Program

#include <stdio.h>

int main()
{

    FILE *file1, *file2, *mergeFile;
    char fileName1[1000];
    char fileName2[1000];
    char mergeFileName[1000];

    char c;
    printf("Enter first file name with full path: ");
    scanf("%s"fileName1);

    printf("Enter second file name with full path: ");
    scanf("%s"fileName2);

    printf("Enter merge file name with full path: ");
    scanf("%s"mergeFileName);

    //open file in read mode
    file1 = fopen(fileName1"r");

    //open file in read mode
    file2 = fopen(fileName2"r");

    if (file1 == NULL || file2 == NULL)
    {

        printf("Error in opening file \n");
        return -1;
    }

    //open file in write mode
    mergeFile = fopen(mergeFileName"w");
    if (mergeFile != NULL)
    {
        printf("File created successfully!\n");
    }
    else
    {

        printf("Failed to create the file.\n");
        // exit status
        return -1;
    }

    // first file
    c = getc(file1);
    while (c != EOF)
    {
        putc(cmergeFile);
        c = getc(file1);
    }
    putc('\n'mergeFile);
    // second file
    c = getc(file2);
    while (c != EOF)
    {
        putc(cmergeFile);
        c = getc(file2);
    }

    //close files
    fclose(file1);
    fclose(file2);
    fclose(mergeFile);

    return 0;
}

Input:

Enter first file name with full path: C:\Users\studentrecord.txt
Enter second file name with full path: C:\Users\hello.txt
Enter merge file name with full path: C:\Users\merge.txt

Output:

File created successfully!
Input File:studentrecord.txt
Name = Rahul, marks = 67 Name = Suman, marks = 65 Name = Jeet, marks = 56 Name = Anuj, marks = 67

Input File: hello.txt

1 2 3 4

Output File: merge.txt
Name = Rahul, marks = 67 Name = Suman, marks = 65 Name = Jeet, marks = 56 Name = Anuj, marks = 67 1 2 3 4


Program to Copy File into Another File

Write a program to copy the content of the file into Another File.
C Program
#include <stdio.h>

int main()
{

    FILE *fromFile, *toFile;
    char fromFileName[1000];
    char toFileName[1000];
    char ch;

    printf("Enter from file name with full path: ");
    scanf("%s"fromFileName);

    printf("Enter to file name with full path: ");
    scanf("%s"toFileName);

    fromFile = fopen(fromFileName"r");
    if (fromFile == NULL)
    {
        printf("Error in opening file \n");
        return -1;
    }
    toFile = fopen(toFileName"w");
    if (toFile != NULL)
    {
        printf("File created successfully!\n");
    }
    else
    {
        printf("Failed to create the file.\n");
        return -1;
    }
    // first file
    ch = getc(fromFile);
    while (ch != EOF)
    {

        putc(chtoFile);
        ch = getc(fromFile);
    }

    //close the files
    fclose(fromFile);
    fclose(toFile);

    return 0;
}

Input:

Enter from file name with full path: C:\Users\studentrecord.txt
Enter to file name with full path: C:\Users\hello.txt
Output:

File created successfully!

Input File:studentrecord.txt

Name = Rahul, marks = 67 Name = Suman, marks = 65 Name = Jeet, marks = 56 Name = Anuj, marks = 67
Output File: hello.txt

Name = Rahul, marks = 67 Name = Suman, marks = 65 Name = Jeet, marks = 56 Name = Anuj, marks = 67


Program to List Files in Directory

Write a program to list files in the directory.
C Program

#include <stdio.h>
#include <dirent.h>

int main()
{

    DIR *d;
    struct dirent *dir;
    char filechar[1000];

    printf("Enter file directory path: ");
    scanf("%s"filechar);

    d = opendir(filechar);
    if (d)
    {
        while ((dir = readdir(d)) != NULL)
        {
            printf("%s\n"dir->d_name);
        }
        closedir(d);
    }
    return 0;
}
Input:

Enter file directory path: C:\Users   
Output:

. .. .849C9593-D756-4E56-8D6E-42412F2A707B 1706073_6CS123.pdf Attachments Book.xlsx Desktop desktop.ini Documents Getting started with OneDrive.pdf Personal Vault.lnk Pictures Presentation.pptx