Question

In: Computer Science

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 text file using fstream object: Besides including the fsrteam header file to your program, there are three points to remember to read from a batch file. First, to make sure there exists a file. Second, make sure the existing file is not emty. Third, open the file as a read mode.

To append text to the existing file: open and existing file as append mode which will append new information at the end of the file if the file is not emty.

Binary files: binary files are readable only by the compiler. A user would not be able to read a binary file.

Your program should read the text file and create the following output:

1. Find the number of words in each line and display it.

2. Find the number of lines and display it.

3. Find the total number of words in the file and display it.

4. Find the number of words that start with capital letters on each line and display it.

5. Find how many time word "file/files" are represented and display it.   

Solutions

Expert Solution

Write a C++ program to create a text file.

#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
ofstream f;
f.open ("file.txt");
f << "Batch files are text files created by programmer.\nThe 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.\nTo do so, include a header filr to your program. \nCreate an object of type fsrteam. Open the file as write mode.Reading from a text file using fstream object: Besides including the fsrteam header file to your program, there are three points to remember to read from a batch file.\n First, to make sure there exists a file.\n Second, make sure the existing file is not emty. Third, open the file as a read mode.\nTo append text to the existing file: open and existing file as append mode which will append new information at the end of the file if the file is not emty.Binary files: binary files are readable only by the compiler.\n A user would not be able to read a binary file.";
f.close();
}

2. Find the number of lines and display it.

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    int lineNumber = 0;
    string line;
    ifstream f("file.cpp");
    while (getline(f, line))
    lineNumber++;
    cout << "Numbers of lines : " << lineNumber<< endl;

}

3. Find the total number of words in the file and display it.

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    ifstream f;
   f.open("file.txt");
   char Word[30];
   int CountWords=0;
   while(!f.eof())
   {
       f>>Word;
       CountWords++;
   }
   cout<<"Number of words-:"<<CountWords;
   f.close();

}

5. Find how many time word "file/files" are represented and display it.   

#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main()
{
ifstream f("file.txt"); //opening text file
int WordCount=0;
char chr[20],c[ ]="file",b[]="files";
while(f)
{
f>>chr;
if(strcmp(chr,c)==0||strcmp(chr,b)==0)
   WordCount++;
}
cout<<"Occurrence of word file="<<WordCount;
f.close();
}


Related Solutions

WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
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.
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....
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
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.
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
Write a C program that can search for a string within a text file, replace it...
Write a C program that can search for a string within a text file, replace it with another string and put results in a new file.
C++ Code You will write a program to process the lines in a text file using...
C++ Code You will write a program to process the lines in a text file using a linked list and shared pointers. You will create a class “Node” with the following private data attributes: • Line – line from a file (string) • Next (shared pointer to a Node) Put your class definition in a header file and the implementation of the methods in a .cpp file. The header file will be included in your project. If you follow the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT