c++ - strcpy c++ 无法从字符串 char* 转换参数 1

标签 c++ strcpy

我正在尝试将 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/

相关文章:

c++ - 单击另一个小部件时将小部件标记为未选中

c++ - 从另一个 vector 分配一个 vector

c++ - 如何在 C++ 的私有(private)结构中获取/设置值?

c++ - 将指向成员函数参数的指针传递给模板参数

比较两个字符串,strcmp 的问题

C编程: Replace an inner string using strcpy?

.NET 编码速度

将命令行字符串复制到数组,valgrind 错误

在 C 编程中将两个字符串合二为一

c++ - 使用 strcpy 时从 'char' 到 'const char*' 的转换无效