![](/img/trans.png)
[英]'strcpy' : cannot convert parameter 2 from 'WCHAR *' to 'const char *
[英]strcpy c++ cannot convert parameter 1 from string char*
我試圖將txt文件*中的單詞放入字符串數組中。 但是strcpy()有一個錯誤。 它是sais:'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();
}
我建議strcpy (p[i], word);
改為p[i] = word;
。 這是C ++的處理方式,並利用了std::string
賦值運算符。
你這里不需要strcpy
。 一個簡單的任務就是這樣做: p[i] = word;
。 strcpy
用於C風格的字符串,它是以空字符結尾的字符數組:
const char text[] = "abcd";
char target[5];
strcpy(target, text);
使用std::string
意味着您不必擔心正確地獲取數組的大小,也不必擔心調用strcpy
函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.