In: Computer Science
I need the code for a C++ program that creates an array of 5000 String objects that will store each word from a text file. The program will read in each word from a file, and store the first 5000 words in the array. The text file should be read in from the command line.
Please find the code below::
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main( )
{
ifstream infile;
string line;//for read line
char fileName[20];
cout<<"Enter file name : ";
cin>>fileName;
cout<<"Loading the
file........"<<endl;
infile.open (fileName);
string words[5000];
int count = 0;
if (infile.is_open())
{
while(infile>>line) { //get
row from text file
if(count<5000) //check limit
{
words[count] = line;
count++;
}
}
infile.close(); //close file
cout<<"File scan
done........"<<endl;
}
else //if file not found show the below message
{
cout << "Sorry, we could not
find the file." << endl;
}
cout<<"Successfully stored
"<<count<<" words to the array";
return 0;
}
readMe.txt
We observe today not a victory
of party but a celebration
of freedom sysbolizing an end
as well as a beginning
signifying renewal as well
as change
output: