gpt4 book ai didi

c++ - Scanf_s 在 C++ 中为空

转载 作者:行者123 更新时间:2023-11-30 01:19:26 25 4
gpt4 key购买 nike

我无法使用此代码,因为我无法编写任何文本。我如何通过阅读解决这个问题?

#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "string.h"




int _tmain(int argc, _TCHAR* argv[])
{
char str[1024];

printf("Input text: ");
scanf_s("%c", str);

_asm
{
//some assembly code
}


printf("\n\nResult = %s", str);

printf("\n\n[Press any key]");
_getch();

return 0;
}

结果是

Input text: sdf

Result =

[Press any key]

有什么想法吗?我使用 Visual Studio 2013。

最佳答案

我不知道函数 scanf_s,所以我查找了 documentation .它指出,与 scanf 相比,scanf_s 需要为格式说明符 cC 指定缓冲区大小, sS 作为通常参数之后的第二个参数。一个例子:

char str[1024];

printf("Input text: ");
scanf_s("%s", str, 1024);

文档还指出,在可能发生缓冲区溢出的情况下,不会向缓冲区写入任何内容。

关于c++ - Scanf_s 在 C++ 中为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20904839/

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