C++ | I/O

Write the program in c++ that the names of the files to read are passed to the program as command line arguments.

The file should be open and close. You have to consider few things once opening the file.

1) If the file can not open print the messages and continue to the next file.

2) If the file is empty then print the messages.

3) The example of input file:

If the file is include this line: I would like to fly, but the great “angel”, is look at me at far a way!

You have to consider few error messages once the input entered.

a) Look at WORDS and PHRASES in the input.

b) When you run your program, we consider phrases of a certain length X, all sequences of X consecutive words. Loot at below to see the list of all of the phrases in the input file for the case of X equal to 3:

I would like

to fly, but

the great “angel”,

is look at

me at far

a way! I

c) You should print the error messages if the case where no arguments are passed foe X.

My current code:


#include <fstream> // for file-access
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
if (argc > 1) {
cout << "argv[1] = " << argv[1] << endl;
} else {
cout << "Can not get file name";
return -1;
}
ifstream infile(argv[1]); //open the file

if (infile.is_open() && infile.good()) {
cout << "File is open!\nContains:\n";
string line = "";
while (getline(infile, line)){
cout << line << '\n';
}

} else {
cout << "Can not open" << endl;
}
return 0;
}


I need help to fix the rest of requirement for my assignment..

Thanks
Last edited on
rather than getline, you might want to just read word by word...

string s;

infile >> s;

and every third time you get a word, output an end of line.
Topic archived. No new replies allowed.