Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Reading File\'s Line , words, chars. There are several error cases that must

ID: 3792189 • Letter: C

Question

C++ Reading File's Line , words, chars.

There are several error cases that must be handled:

A file is not found Print “File filename is not found” and stop

An argument is poorly formed (a known argument is missing the = sign, has nothing after the = sign, or (in the case of findchar, has more than one character after the =) Print “Argument arg is poorly formed” and stop

An argument is not recognized Print “Argument arg is not recognized” and stop

A flag is duplicated Ignore the fact that a particular flag argument is duplicated. Only process each letter or word one time no matter how many times it is specified on the command line

A file name appears more than once The file should be processed each time the name appears in the list 2

Code that I wrote:

#include // for file-access
#include
#include
   #include
using namespace std;
int main(int argc, char* argv[])
{
if (argc > 1)
{
}
else
{
cout << "File anInvalidFileName is not found" << endl;
return -1;
}
ifstream infile(argv[1]); //open the file
if (infile.is_open() && infile.good())
{
string line = "";
int countline = 0;
int charcount = 0;
char space;
int countspace = 0;
int empty = 0;
while (getline(infile, line))
{
if (line.empty())
{
empty++;
}
countline++;
charcount += line.length()+1;
for (int i = 0; i < line.length(); i++)
{
if (line[i] == ' ')
countspace++;
}
}
countspace = (countline-empty) + countspace ;
cout << setw (12) << countline ;
cout << setw(12) << countspace ;
cout << setw(12) << charcount << " " ;
cout << argv[1] << endl;
}
else if (argv[1] == "-thisisunkown=5")
{
cout << "File " << argv[1] << " is not found" << endl;
           return 0;
}
else if (argv[1] == "-findword")
{
cout << "Argument " << argv[1] << " is poorly formed" << endl;
return 0;
}
else
{
cout << "Argument " << argv[1] << " is not recognized" << endl;
}
return 0;
}

This code produces the line, words, and chars. But it does not check the errors correctly. Also I need to be able to read two text files and be able to add the line, word, and chars. Example:

Output:

10 200 1000 infile1

20 150 1010 infile2

30 350 2010 total

I also need the program to find the location of a word/letter and the amount of times the letter / word shows up.

Example :

Run:

program -findword=this infile1

Output:

10 200 1000 infile1

this: 7 12 infile1

It will produce the line,word, and char count on the first line then the amount of tims this showed up. 7 different line and 12 times.

Must handle reading from standard input (stdin).

So what need to be done:

Reading the files ( checking for errors), saving the number of line,words,char then adding them with another text's line, words, and chars and producing a total. ( where I am having the must trouble) . Then have the program be able to find a word or letter and state how many lines it shows up and how many times it shows up. Finialy be able to read from stdin.

argv[1] is the file name the reason i have arg == -findword is because that should produce an error (which it does not), the code I provided works for the first part in finding the size of text 1 and two, but i don't think it would work for the second. The reason i don't think it would work is because i need to be able to read two files and save there infomation, because my code would overrides the data each time. But to answer you question, argv[1] has the file name and once opened the infomation inside is used.

Also, might not have been clear:

The following should produce this error message:

anInvalidFileName - File anInvalidFileName is not found

-thisisunkown=5 - Argument -thisisunkown=5 is not recognized

-findworderror - Argument -findworderror is poorly formed

-findword - Argument -findword is poorly formed

Explanation / Answer

#include <iostream>

#include <fstream> // for file-access

#include <iomanip>

//#include

//#include

using namespace std;

int main(int argc, char* argv[])

{

if (argc >= 1)

{

for(int i=1;i<=argc;i++)

{

ifstream infile(argv[i]); //open the file

if (infile.is_open() && infile.good())

{

string line = "";

int countline = 0;

int charcount = 0;

char space;

int countspace = 0;

int empty = 0;

while (getline(infile, line))

{

if (line.empty())

{

empty++;

}

countline++;

charcount += line.length()+1;

for (int i = 0; i < line.length(); i++)

{

if (line[i] == ' ')

countspace++;

}

}

countspace = (countline-empty) + countspace ;

cout << setw (12) << countline ;

cout << setw(12) << countspace ;

cout << setw(12) << charcount << " " ;

cout << argv[1] << endl;

}

else if (argv[i] == "-thisisunkown=5")

{

cout << "File " << argv[i] << " is not found" << endl;

return 0;

}

else if (argv[i] == "-findword")

{

cout << "Argument " << argv[i] << " is poorly formed" << endl;

return 0;

}

else

{

cout << "Argument " << argv[i] << " is not recognized" << endl;

}   

  

}

}

else

{

cout << "File anInvalidFileName is not found" << endl;

return -1;

}

  

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote