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)
Create this C++ program using classes 1. Create a file text file with a string on...
Create this C++ program using classes 1. Create a file text file with a string on it 2. Check the frecuency of every letter, number and symbol (including caps) 3. Use heapsort to sort the frecuencys found 4. Use huffman code on the letters, symbols or numbers that have frecuencys I created the file, and the frecuency part but i'm having trouble with the huffman and heapsort implementation.
Your assignment is to write a C++ program to read a text file containing the information...
Your assignment is to write a C++ program to read a text file containing the information of the employees of a company, load them into memory and perform some basic human resources operations. This assignment will help you practice: multiple file programming, classes, public and private methods, dynamic memory allocation, constructors and destructors, singly linked list and files. Implementation This lab assignment gives you the opportunity to practice creating classes and using dynamic memory in one of the required classes....
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 java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
In C++, write a program that reads data from a text file. Include in this program...
In C++, write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global variables are the actual data points, the mean, the standard deviation, and the number of data entered. All other variables must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function... ALL LINES...
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
For C++ Write a program that opens a specified text file then displays a list of...
For C++ Write a program that opens a specified text file then displays a list of all the unique words found in the file. Hint: Store each word as an element of a set.
Write a C program to run on unix to read a text file and print it...
Write a C program to run on unix to read a text file and print it to the display. It should count of the number of words in the file, find the number of occurrences of a substring, and take all the words in the string and sort them (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring] filename • The -c flag...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT