4

txtファイル*にある単語を文字列の配列に入れようとしています。しかし、strcpy() にはエラーがあります。'strcpy' : パラメータ 1 を 'std::string' から 'char *' に変換できません。何故ですか?このような文字列の配列を C++ で作成することはできませんか?

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

void ArrayFillingStopWords(string *p);

int main()
{
   string p[319];//lekseis sto stopwords
   ArrayFillingStopWords(p);
   for(int i=0; i<319; i++)
   {
       cout << p[i];
   }
   return 0;
}

void ArrayFillingStopWords(string *p)
{
    char c;
    int i=0;
    string word="";
    ifstream stopwords;
    stopwords.open("stopWords.txt");
    if( stopwords.is_open() )
    {
       while( stopwords.good() )
       {
           c = (char)stopwords.get();
           if(isalpha(c))
           {
               word = word + c;
           }
           else
           {
               strcpy (p[i], word);//<---
               word = "";
               i++;
           }
       }
   }
   else
   {
       cout << "error opening file";
   }
   stopwords.close();
}
4

2 に答える 2

3

strcpy (p[i], word);に変更することをお勧めしますp[i] = word;。これは、std::string代入演算子を利用する C++ の方法です。

于 2013-04-14T15:34:35.870 に答える
0

ここは必要ありませんstrcpy。簡単な代入でそれができます: p[i] = word;. strcpyこれは、null で終わる文字配列である C スタイルの文字列用です。

const char text[] = "abcd";
char target[5];
strcpy(target, text);

を使用std::stringすると、配列のサイズを正しく取得したり、 のような関数を呼び出したりすることについて心配する必要がなくなりますstrcpy

于 2013-04-14T15:38:22.590 に答える