Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.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++_Ifstream_Void_Words - Fatal编程技术网

C++ 读取文本文件,然后将字符添加到列表中?

C++ 读取文本文件,然后将字符添加到列表中?,c++,ifstream,void,words,C++,Ifstream,Void,Words,因此,我正在尝试完成程序的这一部分,我必须从Stdin中读取一个文本文件,并将其添加到“单词列表”wl中。我知道如何从文本文件中读取,但如果有意义的话,我不知道如何在列表中添加“单词”。下面是我得到的: string getWord(){ string word; while (cin >> word){ getline(cin, word); } return word; } void fillWordList(string sou

因此,我正在尝试完成程序的这一部分,我必须从Stdin中读取一个文本文件,并将其添加到“单词列表”wl中。我知道如何从文本文件中读取,但如果有意义的话,我不知道如何在列表中添加“单词”。下面是我得到的:

string getWord(){
    string word;
    while (cin >> word){
        getline(cin, word);
    }
    return word;
}

void fillWordList(string source[], int &sourceLength){
    ifstream in.file;
    sourceLength = 50;
    source[sourceLength]; ///this is the part I'm having trouble on
Source是一个数组,用于确定从文本中读取多少单词,length是屏幕上打印的数量

关于我应该从什么开始有什么想法吗

编辑:以下是我为之编写实现的程序:

#include <iostream>
#include <string>
#include <vector>
#include "ngrams.h"
void help(char * cmd) {
  cout << "Usage: " << cmd << " [OPTIONS] < INPUTFILE" << endl;
  cout << "Options:" << endl;
  cout << "  --seed RANDOMSEED" << endl;
  cout << "  --ngram NGRAMCOUNT" << endl;
  cout << "  --out OUTPUTWORDCOUNT" << endl;
}
string source[250000];
vector<string> ngram;
int main(int argc, char* argv[]) {
  int n, outputN, sl;
  n = 3;
  outputN = 100;
  for (int i = 0; i < argc; i++) {
    if (string(argv[i]) == "--seed") {
      srand(atoi(argv[i+1]));
    } else if (string(argv[i]) == "--ngram") {
      n = 1 + atoi(argv[i+1]);
    } else if (string(argv[i]) == "--out") {
      outputN = atoi(argv[i+1]);
    } else if (string(argv[i]) == "--help") {
      help(argv[0]);
return 0; }
  }
  fillWordList(source,sl);
  cout << sl << " words found." << endl;
  cout << "First word: " << source[0] << endl;
  cout << "Last word:  " << source[sl-1] << endl;
  for (int i = 0; i < n; i++) {
    ngram.push_back(source[i]);
  }
  cout << "Initial ngram: ";
  put(ngram);
  cout << endl;
  for (int i = 0; i < outputN; i++) {
    if (i % 10 == 0) {
  cout << endl;
}
//put(ngram);
//cout << endl;
cout << ngram[0] << " ";
findAndShift(ngram, source, sl);
} }
#包括
#包括
#包括
#包括“ngrams.h”
无效帮助(char*cmd){

cout声明原始数组要求数组的大小为编译时常量。请使用
std::vector
或至少使用
std::array
代替。如果要填充它,请通过引用传递
source

这两个函数之间的关系不清楚。我也不太确定要对源代码做什么和sourceLength。@对它们持怀疑态度的Jule,我对如何处理它们也有点困惑。下面是标题中对我应该做什么的描述:
string getWord();//从标准输入返回一个空格分隔的单词
void fillWordList(string source[],int&sourceLength);//从标准输入读取文本文件,并将所有//空格分隔的单词添加到单词列表wl