简体   繁体   English

如何从文件中读取单词,将它们分配给数组并分析其内容?

[英]How do I read words from a file, assign them to an array and analyze its content?

I (a student whose professor encourages online research to complete projects) have an assignment where I have to analyze the contents of a file (frequency of certain words, total word cout, largest and smallest word) and I'm getting stuck on even opening the file so the program can get words out.我(一位教授鼓励在线研究完成项目的学生)有一项作业,我必须分析文件的内容(某些单词的频率、总单词数、最大和最小单词),我什至无法打开文件,以便程序可以输出单词。 I've tried to just count the words that it reads and i get nothing.我试着只计算它读到的单词,但我什么也没得到。 As I understand it, the program should be opening the selected .txt file, going through its contents word by word and outputing it right now.据我了解,该程序应该打开选定的 .txt 文件,逐字浏览其内容并立即输出。

Here's code:这是代码:

#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
#include <sstream>

    string selected[100];
    //open selected file.
    ifstream file;
    file.open(story.c_str());
    string line;
    if (!file.good())
    {
        cout << "Problem with file!" << endl;
        return 1;
    }
    while (!file.eof())
    {
        getline(file, line);

        if (line.empty())
            continue;

        istringstream iss(line);

        for (string word; iss >> word;)
            cout << word << endl;

    ```




Because of the simplicity of the attached code, I will not give detailed explanations here.由于所附代码比较简单,这里就不做详细说明了。 With the usage of std::algorithm every task can be performed in a one-liner.通过使用std::algorithm每个任务都可以单线执行。

We will read the complete source file into one std::string .我们将完整的源文件读入一个std::string Then, we define a std::vector and fill it with all words.然后,我们定义一个std::vector并用所有单词填充它。 The words are defined by an ultra simple regex.这些词是由一个超简单的正则表达式定义的。

The frequency is counted with a standard approach using std::map .使用std::map使用标准方法计算频率。

#include <fstream>
#include <string>
#include <iterator>
#include <vector>
#include <regex>
#include <iostream>
#include <algorithm>
#include <map>

// A word is something consiting of 1 or more letters
std::regex patternForWord{R"((\w+))"};

int main() {

    // Open file and check, if it could be opened
    if (std::ifstream sampleFile{ "r:\\sample.txt" }; sampleFile) {

        // Read the complete File into a std::string
        std::string wholeFile(std::istreambuf_iterator<char>(sampleFile), {});

        // Put all words from the whole file into a vector
        std::vector<std::string> words(std::sregex_token_iterator(wholeFile.begin(), wholeFile.end(), patternForWord, 1), {});

        // Get the longest and shortest word
        const auto [min, max] = std::minmax_element(words.begin(), words.end(),
            [](const std::string & s1, const std::string & s2) { return s1.size() < s2.size(); });

        // Count the frequency of words
        std::map<std::string, size_t> wordFrequency{};
        for (const std::string& word : words) wordFrequency[word]++;

        // Show the result to the user
        std::cout << "\nNumber of words: " <<  words.size() 
            << "\nLongest word: " << *max << "  (" << max->size() << ")"
            << "\nShortest word: " << *min << "  (" << min->size() << ")"
            << "\nWord frequencies:\n";
        for (const auto& [word, count] : wordFrequency) std::cout << word << " --> " << count << "\n";

    }
    else {
        std::cerr << "*** Error:  Problem with input file\n\n";
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++如何从文件中读取数字并将其分配给数组中的相应位置? - C++ How do I read numbers from a file and assign it to a respective place in an array? 如何从文件中读取数字并在数组中使用它们? - How can I read numbers from an file and use them in an array? 如何确保从文件中读取的单词是我希望它们成为C ++的方式 - How to make sure the words being read in from the file are how I want them to be C++ 如何从文件中读取并将内容存储在动态二维数组中并以网格格式显示? - How do I read from a file and store the content in a dynamic 2d array and display it in a grid format? 如何正确读取文本文件中的单词并反转偶数单词的顺序? - How do I correctly read words from a text file and reverse the order of even words? 如果我只想阅读数字,如何忽略文本文件中的单词 - How do I ignore words from a text file if I only want to read the numbers 如何将数据从文本文件读取到结构数组中 - How do I read data from a text file into an array of struct 如何从文件读入二维数组? - How do I read from a file into a 2d array? 如何从文件中读取结构数组并对其进行排序? - How do I read an array of structs from a file and sort it? 如何读取和输出文件的内容及其包含的单词数? - How Do I read and Output the Contents of a File and the Number of Words it Contains?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM