Write a C++ program to create a text file. Your file should contain the followin
ID: 3750751 • Letter: W
Question
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 the notepad. Creating a text file and writing to it by using fstream: to write a file, you need to open the file as write mode. To do so, include fstream header file to your program. Create an object of type fstream. Open the file as write mode. Reading from a text file using fstream object: Besides including the fstream header file to your program, there are three points to remember to read from a batch file. First, to make sure there exist a file. Second, Make sure that the existing file is not empty. Third, open the file as a read mode. To append text to the existing file: Open an existing file as append mode which will append new information at the end of the file if the file is not empty Binary files: Binary files are readable only by 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. 2. 3. 4. 5. Find number of words in each line and display it. Find number of lines and display it. Find total number of words in the file and display it. Find number of words starts with capital letter and each line and display it. Find that how many tiles the word "file/files" are is repeated and display it.Explanation / Answer
#include<iostream>
#include<fstream>
#include<cstring>
int main()
{
std::ifstream fin("input.txt"); //opening text file
int line=1,word=1,t_word=1,upper=0;
char ch;
while(fin)
{
fin.get(ch);
if(ch==' ' || ch==' ')
word++; // for every space(‘ ‘ && ' ') in the string, count is incremented.
if(ch==' ')// for every space(‘ ‘) in the string, count is incremented.
line++;
if(ch==' ') // for every space(‘ ‘ && ' ') in the string, count is incremented.
t_word++;
if(ch>='A' && ch<='Z')//If any word is uppercase letter the count is incremented count is incemented
upper++;
}
std::cout<<"No.of words in each line="<<word;
std::cout<<" No.of Lines="<<line;
std::cout<<" Total np.of words="<<t_word;
std::cout<<" Total no.of capital letters="<<upper;
fin.close(); //closing file
return 0;
}
I'm solved the first 4 questions in the above and the last 5th one is not clear to me.
I hope you understand..
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.