将调用从.txt转换为字符串c ++

时间:2016-02-06 05:43:32

标签: c++

我试图从文本文件中读取数据,然后计算每个单词的出现次数,然后将结果导出到另一个文本文件中。我只允许使用循环和数组进行此分配。我只是想在正确的方向上轻轻推动,主要是在代码的开头。它没有正确编译!

using namespace std;
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>


void put(string word, string words[], int counts[], int & size)

{
 int w;
 for (w = 0; w < size && word >= words[w]; w++);
 if (w >= size) {
 words[w] = word;
 counts[w] = 1;
 ++size;
}
else if (word < words[w]) {

for (int i = size - 1; i >= w; i--) {
 words[i + 1] = words[i];
 counts[i + 1] = counts[i];
}
words[w] = word;
counts[w] = 1;
++size;
}
else {
counts[w] = counts[w] + 1;
}
}

int main()
{
 int word;

 ifstream input("input.txt");
 ofstream chout("charcount.txt");
 ofstream wout("wordscounts.txt");


 int inputSize = sizeof(input) / sizeof(string);
 int counts[100];
 const int MAX = 100;
 string words[MAX];
 int wordsSize = 0;


 while (input >> word) {
  put(input[word], words, counts, wordsSize);
 }



 wout << "       Word  Frequency" << endl;

for (word = 0; word < inputSize; ++word) {
 wout << setw(10) << words[word] << setw(4) << counts[word] << endl;
 }




chout.close();
wout.close();

system("pause");
return 0;
}

1 个答案:

答案 0 :(得分:0)

这一行:

put(input[word], words, counts, wordsSize);

您正在尝试索引ifstream。这根本不允许。编译器(g ++ 4.8.4)说:

no match for ‘operator[]’ (operand types are ‘std::ifstream {aka std::basic_ifstream<char>}’ and ‘int’)