In C Write a program to read in a simple text file lets call
In C++, Write a program to read in a simple text file (let\'s call it \"simplefile.txt\") and:
find the number of chars in each word (save those values in an array called \"word\"), the number of chars in each line (save those values as an array called \"line\"), the number of chars in each sentence (save those values as an array called \"sent\"), and the number of chars in each paragraph (save those values as an array called \"para\").Please make the program as simple as possible and include comments illustrating the logic behind each step.
Solution
I am going to share the code of the given question if you face any trouble in compiling the code or in understanding of the code feel free to ask in the comment section below.
#include<iostream.h>
#include<fstream.h>
int main()
{
ifstream fin(\"file.txt\"); // To open the text file
int l=1,w=1,s, chars=1; //First word and last line will not be counted that is why we are initializing value of line and word as 1, here w stands for words and l for lines
char c;
fin.seekg(0,ios::end); //point the file pointer at the end of the file
size=fin.tellg(); // For counting number of bytes till current position of the pointer fin.seekg(0,ios::beg); //Point at the beginning of the file
while(fin)
{
fin.get(c);
chars++;
if(c==\'n\'||c==\' \')
w++;
if(c==\'n\')
l++;
}
cout<<”Characters=”<<chars<<\"nWords=\"<<w<<\"nLines=\"<<l <<\"n\";
fin.close(); //File is closed
return 0;
}
