简体   繁体   English

我将如何 go 关于使用 C++ 将纯文本文件中的单词存储到数组中?

[英]How would I go about storing words from a plain text file to an array using C++?

I've been tasked with writing a C++ program that opens a text file, determines the length of each word, then produces output stating how many times a particular word length occurs.我的任务是编写一个 C++ 程序,该程序打开一个文本文件,确定每个单词的长度,然后生成 output 说明特定单词长度出现了多少次。
I've figured how to open and read the contents of the file.我已经想出了如何打开和读取文件的内容。

How would I take each word and store them in an array?我将如何获取每个单词并将它们存储在数组中?

#include <iostream>
#include <fstream>
#include <string>

using namespace std;


void getFile(string);

void getFile(string filename)
{
    string array[2]; 
    short loop = 0; 
    string line; 
    ifstream myfile (filename); 
    if (myfile.is_open()) 
    {
        while (!myfile.eof() ) 
        {
            getline (myfile,line); 
            array[loop] = line;
            cout << array[loop] << endl; 
            loop++;
        }
        myfile.close(); 
    }
    else{
        cout << "can't open the file"; 
        system("PAUSE");
    } 
    
}

int main(){
    string fileName;
    while (true){
        cout << "\nEnter the name of a file: ";
        getline(cin, fileName);
        
        if (fileName == ""){
            cout << "Invaled file name, enter another!!!"<<endl;
            main();
        }
        else{
            getFile(fileName);
        }
    }
    
    return 0;
}

You do not store words in an array.您不会将单词存储在数组中。
You only need to store the word lengths and how often each of them occurred.您只需要存储单词长度以及每个单词出现的频率。
If you have a guaranteed and low maximum word length you can even simplify by using an array where the length of the current word is used as an index.如果您有一个保证且较低的最大字长,您甚至可以通过使用一个数组来简化,其中当前字的长度用作索引。 Init all entries with 0. Then count entries up when the corresponding word length occurs.用 0 初始化所有条目。然后在出现相应的字长时将条目计数。

暂无
暂无

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

相关问题 如何替换命令提示符中已经存在的文本? C ++ - How would I go about replacing text already on the command prompt? C++ C ++-如何制作一个简单的计时器,以及如何基于它使用if语句? - C++ - How to make a simple timer and how would I go about using if statements based on it? 我将如何从C ++中的预处理器调用函数? - How would I go about calling a function from a preprocessor in C++? 我将如何从 for 循环中找到一组整数的平均值(在 C++ 中) - How would I go about finding the average of a set of integers from a for loop (in C++) 我将如何 go 将 AOB 从作弊引擎修改为 C++? - How would I go about modifying an AOB from Cheat Engine to C++? 一个 go 如何在 C++ 中“链”管道超过 2 个管道? 目前,我在第二个命令中收到来自 Bash 的错误文件描述符错误 - How would one go about 'chain' piping more than 2 pipes in C++? Currently I am getting the Bad file descriptor error from Bash on the second command 我应该如何读入一个文本文件,其中每一行都包含C ++中的各种数据类型? - How should I go about reading in a text file with each line containing various data types in C++? 在C ++中,我如何使用指针获取数组的平均值? - In c++ how do I go about using pointers to get the average of an array? 将文本文件中的数据存储在结构数组C ++中 - Storing data from a text file in an array of structures C++ 从文本文件读取并存储到数组C ++ - Read from a text file and storing to an array C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM