我正在尝试将 txt 文件* 中的单词放入一个字符串数组中。 但是 strcpy() 有错误。它说: 'strcpy' : cannot convert parameter 1 from 'std::string' to '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();
}
最佳答案
我建议将strcpy (p[i], word);
改为p[i] = word;
。这是 C++ 的处理方式,并利用了 std::string
赋值运算符。
关于c++ - strcpy c++ 无法从字符串 char* 转换参数 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16000649/