Well, I am working on a homework assignment, where I am to print out, on screen, the longest word from list of words from a file on a server. The server is a Unix/Linux (?) server (bash), with the following pathway:
/users/dwynne/resources/american-english-large
For the life of me, I have yet to find any references on how to refer to a file in this manner. I know that you should do the following for a specific file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int main (){
std::string word;
ifstream dictionaryFile ("/users/dwynne/resources/american-english-large");
if (dictionaryFile.is_open()){
while(dictionaryFile >> word){
std::cout << word << endl;
}
file.close();
}
else
std::cout << "ERROR! File not found!" << endl;
}
system ("PAUSE");
return 0;
}
The only thing I need is to be point in the right direction. Is there a book or reference that I can go to, in order to read up some more about how to do this? Ultimately, I want to input data from a file the longest word, where I can then output this word onto the terminal's screen. Again, I just need to be pointed in the right direction, so that I can learn this stuff. Until then, thanks.
int main (){
std::string word;
ifstream dictionaryFile ("/users/dwynne/resources/american-english-large");
if (dictionaryFile.is_open()){
while(dictionaryFile >> word){
std::cout << word << endl;
dictionaryFile.close();
}
}
else{
std::cout << "ERROR! File not found!" << endl;
}
return 0;
}
It's amazing how forgetting to put the brackets in their proper place can screw things up.
Though the code is "correct", but I still need to figure out how to output the longest word from a list of words from another file on a server onto the screen. I wanted to know if there are any resources as to how to do this. So far, I am outputting the SHORTEST word, not the LONGEST word. I think arrays and loop counts are involved in the process, but I am not sure. I apologies for rambling, but it's been a long day, and my brain is "fried". Maybe I will rest up, and try again tomorrow...
One thing just to give you a push in the right direction is you are closing your dictionaryFile too soon. You are closing your file after you read 1 word from the file, but you actually want to close the file once you are done reading from the file.
Instead of
1 2 3 4 5
while (dictionaryFile >> word)
{
std::cout << word << endl;
dictionaryFile.close();
}
do
1 2 3 4 5
while (dictionaryFile >> word)
{
std::cout << word << endl;
}
dictionaryFile.close();
So, the initial program works. I want to add something else, in order to count the length of the longest word, and then display it on the terminal screen:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int main (){
std::string longestWord; // Longest word from file.
ifstream wordFile ("/users/dwynne/resources/american-english-large");
if (wordFile.is_open()){
while(wordFile >> longestWord){
for (int i=0; i<longestWord; i++){ // What I had added.
std::cout << longestWord << endl;
}
}
dictionaryFile.close();
}
else{
std::cout << "ERROR! File not found!" << endl;
}
return 0;
}
You can't believe the error I had gotten (two+ pages worth, in fact).
Anyway, it was my impression that by adding "for" loop, I will get the maximum length of the word from the file. Did I miss anything?