# include & lt; iostream>
# include & lt; Fstream>
# include & lt; Map>
# include "algorithm"
Bool ispunct (STD: : string word) {
Int len=word length ();
If (word [len - 1]=='. '| | word [len - 1]=='? '| | word [len - 1]==', '| | word [len - 1]=='! ') {
return true;
}
return false;
}
Int main () {
STD: : cout & lt; <"Enter the file name:";
STD: : string filename, word;
//STD: : cin & gt;> The filename.
Filename="mytest. TXT";
STD: : cout
STD: : fstream filestream;
Filestream. Open (filename. C_str ());
/* if (! Filestream. Is_open ()) {
STD: : cout & lt; <"No open & lt;"
While (filestream & gt;> Word) {
If (ispunct (word)) {
Word=word. Substr (0, word length () - 1);
}
/* this transform is to put all the inside of the letters converted to lower case
* STD: : transform (word. The begin (), the word. The end (), the word. The begin (), [] (unsigned char c) {
Return STD: : tolower (c); }); */
STD: : transform (word. The begin (), the word. The end (), the word. The begin (), : : tolower);
The map [word] + +;
}
STD: : cout & lt; <"The Frequency of words in file:" & lt;
For (auto el: map) {
STD: : cout & lt;
return 0;
}
Is from TXT file inside to find words, read later determine the number of the same words, I write my own TXT file nowhere, filestream. Is_open () is false. I look at the others code also just write the name of the file, my side is to preach the absolute path? My computer is Mac, can you help me to look at, thank you
CodePudding user response:
Can't open it, then put the errno print it out and see what reason, or use perror function