In: Computer Science
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.
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();
}