Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C+将纯文本文件中的单词存储到数组中+;? 我已经编写了一个C++程序,打开一个文本文件,确定每个单词的长度,然后产生一个特定单词长度的输出。 我已经想出了如何打开和读取文件的内容_C++ - Fatal编程技术网

如何使用C+将纯文本文件中的单词存储到数组中+;? 我已经编写了一个C++程序,打开一个文本文件,确定每个单词的长度,然后产生一个特定单词长度的输出。 我已经想出了如何打开和读取文件的内容

如何使用C+将纯文本文件中的单词存储到数组中+;? 我已经编写了一个C++程序,打开一个文本文件,确定每个单词的长度,然后产生一个特定单词长度的输出。 我已经想出了如何打开和读取文件的内容,c++,C++,如何将每个单词存储在一个数组中 #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

如何将每个单词存储在一个数组中

#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;
}
#包括
#包括
#包括
使用名称空间std;
void getFile(字符串);
void getFile(字符串文件名)
{
字符串数组[2];
短回路=0;
弦线;
ifstream myfile(文件名);
如果(myfile.is_open())
{
而(!myfile.eof())
{
getline(myfile,line);
数组[循环]=行;

不能将单词存储在数组中。
您只需要存储单词长度以及每个单词出现的频率。

如果您有一个有保证且较低的最大字长,您甚至可以通过使用一个数组来简化,该数组将当前字长用作索引。使用0初始化所有条目。然后在出现相应的字长时对条目进行计数。

您不在数组中存储单词。
您只需要存储单词长度以及每个单词出现的频率。

如果您有一个保证的、较低的最大字长,您甚至可以通过使用一个数组来简化,该数组将当前字长用作索引。使用0初始化所有条目。然后在出现相应字长时对条目进行计数。

您可以避免存储每一个单词,而可以通过执行
字符串一个单词;我的文件>>一个单词;
。找到它的大小,找到此大小的单词总数,将该计数增加1
,而(!myfile.eof())
->
而(getline(myfile,line))
不相关,但此代码明确调用了
main
。标准明确禁止这样做,您没有理由这样做。另一个(问题)?
void getFile(string)的前声明使用;
是因为您可以在之后定义它。您可以避免存储每一个单词,而是可以通过执行
字符串一个单词;myfile>>一个单词;
来重复提取单个单词。找到它的大小,找到此大小的单词总数,将该计数增加1
,同时(!myfile.eof())
->
while(getline(myfile,line))
不相关,但此代码明确地调用了
main
。标准明确禁止这样做,并且您没有理由这样做。另一个(问题)?
void getFile(string);
的前向声明是可用的,因为您随后就定义了它。