Question

In: Computer Science

Write a C program to find out the number of words in an input text file...

Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file.

Solve in C programming.

Solutions

Expert Solution

#include<stdio.h>
#include<stdlib.h>

int main(){
        char ch,path[50],cpath[50];
        FILE *file,*cfile;
        int count=0;
        
        //Enter the source path of the file
        printf("Enter source file path: ");
    scanf("%s", path);

        
        
        file=fopen(path,"r"); 
        
        if (file == NULL)
    {
        printf("\nUnable to open file.\n");
        printf("Please check if file exists and you have read privilege.\n");

        return 0;
    }
        
        //Gets each character till end of file is reached  
    while((ch = fgetc(file)) != EOF){  
        //Counts each word  
        if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
                count++;
    } 
        printf("\nNumber of words given in the text file is : %d\n",count+1);
        fclose(file);
        
        
    //Give a file name to the copied file
    printf("\nEnter the name of the new file :");
    scanf("%s",cpath);
    
    //if file not present, automatically file gets created
    cfile=fopen(cpath,"w");
    file=fopen(path,"r");
    while ((ch = fgetc(file)) != EOF)
      fputc(ch, cfile);

    printf("\nCopy of file created\n");
        //closing both files
        
    fclose(file);
    fclose(cfile);
    
    return 0;
}

OUTPUT:

I have created a text fie named "in" in my dekstop. Then with the given code i have made a copy of "in" file into "copy_in" file


Related Solutions

In C++, write a program that accepts a text file of ASCII words from standard input...
In C++, write a program that accepts a text file of ASCII words from standard input and store them and the amount of times the word appears in the file in a hash table using external chaining. Then print the words and their counts sorted based on alphabetical order and print them again in decreasing numerical order based on the amount of times the word appears in the file. Space, tab, and new line all count as space characters. The...
Java Write a method that reads a text file and prints out the number of words...
Java Write a method that reads a text file and prints out the number of words at the end of each line
Done in C++, Write a program to read the input file, shown below and write out...
Done in C++, Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets 6 Philadelphia 31, Tampa Bay 20 Green Bay 19,...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER IS PREFERRED THANK YOU IN ADVANCE...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. The whole program is under 40 lines of code. Just read from cin. Now, you need to detect non integers. In this case,...
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. Remember, try not to do the entire job all at once! First try input of a single number and make sure it works....
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT