gpt4 book ai didi

c++ - 使用 strcmp 比较两个字符串的问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:04:37 34 4
gpt4 key购买 nike

我想比较 2 个字符串,但是当我执行 strcmp 函数时,它告诉我:

'strcmp' : cannot convert parameter 1 from 'std::string'

我该如何解决这个问题?

这是我的代码:

int verif_file(void)
{
string ligne;
string ligne_or;

ifstream verif("rasphone");
ifstream original("rasphone.pbk");
while (strcmp(ligne, "[SynCommunity]") != 0 &&
(getline(verif, ligne) && getline(original, ligne_or)));
while (getline(verif, ligne) && getline(original, ligne_or))
{
if (strcmp(ligne, ligne_or) != 0)
return (-1);
}

return (0);
}

最佳答案

你的编译器给你一个错误,因为 strcmp是一个 C 风格的函数,需要 const char* 类型的参数,并且没有从 std::stringconst char* 的隐式转换.

虽然您可能会使用 std::stringc_str() 方法检索这种类型的指针,因为您正在使用 std::string对象,您应该改用运算符==:

if (ligne == ligne_or) ...

或与const char*比较:

if (ligne == "[Syn****]") ...

关于c++ - 使用 strcmp 比较两个字符串的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19183971/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com