【问题标题】:Does not work _cgets. Identifier not found_cgets 不起作用。找不到标识符
【发布时间】:2021-11-22 07:30:39
【问题描述】:

我正在尝试在 Visual Studio 2019 中编译以下代码:

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <string>
int main() 
{
     char buffer[5];
    char* p;
    buffer[0] = 3;
    p = _cgets(buffer); // problem with _cgets
    printf("\ncgets read %d symbols: \"%s\"\n", buffer[1], p);
    printf("A pointer is returned %p, buffer[2] on %p\n", p, &buffer);

return 0;
}

但我看到以下错误:

main.cpp(11,9): error C3861: '_cgets': identifier not found

【问题讨论】:

  • @AlanBirtles 当我将缓冲区 [1]、p 替换为 p、缓冲区 [1] 时出现错误。因为指针不能转换为十进制。我对你的理解正确吗?函数结束后缓冲区[1]是否存储第二个输入字符的代码?
  • @AlanBirtles 我的内存中有五个单元格。 buffer[0] 存储字符串的最大长度,在buffer[1] 中包含要读取的元素数。 buffer[2] 之后的单元格实际上是用户输入的符号。并在最后一个单元格中\0
  • @AlanBirtles,当返回 str [1] 时包含读取的数字字符。因此,如果您输入例如 "H",则在 buffer[1] 中存储值 0,尽管逻辑上它应该是 1。或者如果您输入 "Hi""Hii""Hiii" buffer[1] 包含 105 个字符而不是分别为 2、3、4。为什么?
  • @AlanBirtles 我不明白您想要的最小可重现示例是什么。不可能更详细地描述这个问题。你需要一个代码吗?它更高,你需要一个问题吗?它也更高还有什么?此外,这个特性是_cgets 所固有的,但是当我用_cgets 替换gets_s 时它会抛出一个错误。 Error (active) E0020 identifier "_cgets" is undefined
  • p = _cgets(buffer); 我有问题 -> Severity Code Description Project File Line Suppression State Error (active) E0020 identifier "_cgets" is undefinedSeverity Code Description Project File Line Suppression State Error C3861 '_cgets': identifier not found ConsoleApplication1 。编译器版本为 19.29.30133.0。链接库:#include &lt;iostream&gt;#include &lt;stdio.h&gt;#include &lt;math.h&gt;#include &lt;conio.h&gt;#include &lt;string&gt;

标签: c++ string


【解决方案1】:

_cgets 不再可用,请改用_cgets_s

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <string>
int main()
{
    char buffer[5];
    size_t sizeRead;
    auto error = _cgets_s(buffer, &sizeRead);
    printf("\ncgets read %zu symbols: \"%s\"\n", sizeRead, buffer);

    return 0;
}

或者更简单的 c++ 代码:

#include <iostream>
#include <string>

int main()
{
    std::string buffer;
    std::getline(std::cin, buffer);
    std::cout << "read " << buffer.size() << " characters \"" << buffer << "\"\n";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 2015-03-01
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多