#include <iostream>
#include <iostream>
#include <fstream>
#include <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 << "Enter file name: ";
std::string filename, word;
// std::cin >> filename;
filename = "mytest.txt";
std::cout<<filename<<std::endl;
std::map<std::string, int> map;
std::fstream filestream;
filestream.open(filename.c_str());
/* if(!filestream.is_open()){
std::cout << "No open"<<std::endl;
}*/
while (filestream >> word) {
if (ispunct(word)) {
word = word.substr(0, word.length()-1);
}
/* 這邊的transform是把里面的字母全部轉換成小寫
* std::transform(word.begin(), word.end(), word.begin(),[](unsigned char c){
return std::tolower(c); });*/
std::transform(word.begin(), word.end(), word.begin(), ::tolower);
map[word]++;
}
std::cout << "Frequency of words in file: "<<std::endl;
for (auto el : map) {
std::cout << el.first << " " << el.second << std::endl;
}
return 0;
}
就是從txt檔案里面找單詞,讀完以后判斷同一個單詞的數量。我自己寫的txt檔案根本打不開,filestream.is_open()也是false.我看別人代碼也只是寫個檔案的名字。我這邊的話是要傳絕對路徑嗎? 我電腦是Mac,能不能幫我看一眼。謝謝
uj5u.com熱心網友回復:
打不開的話,就把 errno 列印出來看看是什么原因,或者用 perror 函式轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/268358.html
標籤:非技術區
上一篇:為啥在vscode中寫的這段代碼在終端列印不出來?電腦是M1的macbook
下一篇:(麻煩給出修改后的代碼)以下程式為單鏈表對多項式求和,為什么會出現debug assertion failed,要怎么修改。急!謝謝
