我试图让一个程序让用户输入一个单词或字符,存储它,然后打印它,直到用户再次键入它,退出程序.我的代码看起来像这样:
#include <stdio.h>
int main()
{
char input[40];
char check[40];
int i=0;
printf("Hello!\nPlease enter a word or character:\n");
gets(input);
printf("I will now repeat this until you type it back to me.\n");
while (check != input)
{
printf("%s\n", input);
gets(check);
}
printf("Good bye!");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是我不断打印输入字符串,即使用户输入(检查)与原始(输入)匹配.我比较错误吗?
我写了这个函数来从文件中读取一行:
const char *readLine(FILE *file) {
if (file == NULL) {
printf("Error: file pointer is null.");
exit(1);
}
int maximumLineLength = 128;
char *lineBuffer = (char *)malloc(sizeof(char) * maximumLineLength);
if (lineBuffer == NULL) {
printf("Error allocating memory for line buffer.");
exit(1);
}
char ch = getc(file);
int count = 0;
while ((ch != '\n') && (ch != EOF)) {
if (count == maximumLineLength) {
maximumLineLength += 128;
lineBuffer = realloc(lineBuffer, maximumLineLength);
if (lineBuffer == NULL) {
printf("Error reallocating space for …Run Code Online (Sandbox Code Playgroud) 我经常看到人们不鼓励其他人使用scanf并说有更好的选择。但是,我最终看到的只是“不要使用scanf”或“这里是正确的格式字符串”,并且从来没有提到“更好的替代方案”的任何示例。
例如,让我们看一下这段代码:
scanf("%c", &c);
Run Code Online (Sandbox Code Playgroud)
这将读取最后一次转换后留在输入流中的空白。通常建议的解决方案是使用:
scanf(" %c", &c);
Run Code Online (Sandbox Code Playgroud)
还是不使用scanf。
由于scanf不好,用于转换scanf通常无需使用即可处理的输入格式(例如整数,浮点数和字符串)的ANSI C选项有哪些scanf?
C中的"坏"功能是什么,它们的"好"选择是什么?
为什么坏的不好,是什么让好的更好?
我知道,例如,它gets()是"坏的",因为它没有任何形式的边界检查.什么是更好的选择?gets()?
我听说gets()很糟糕,但我不记得为什么.谁知道?什么是最好的选择?
还有更多吗?
如果我不知道这个词有多长,我就不会写char m[6];,
这个词的长度可能是十到二十个.如何使用scanf键盘输入?
#include <stdio.h>
int main(void)
{
char m[6];
printf("please input a string with length=5\n");
scanf("%s",&m);
printf("this is the string: %s\n", m);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
请输入一个lenght = 5
的字符串
你好
这是字符串:hello
"普通人不想自由.他只是想要安全." - HL Menken
我正在尝试编写非常安全的C.下面我列出了一些我使用的技术,并且询问它们是否像我认为的那样安全.请不要犹豫将我的代码/先入之见撕成碎片.任何能找到最微不足道的漏洞或者教我一个新想法的答案都会受到高度重视.
根据GNU C编程教程 getline:
getline函数将根据需要通过realloc函数自动扩大内存块,因此永远不会缺少空间 - 这是getline如此安全的一个原因.[..]请注意,无论多长时间,getline都可以安全地处理您的输入线.
我假设getline应该在所有输入下防止从流中读取时发生缓冲区溢出.
如果malloc遇到错误,malloc将返回NULL指针.这会带来安全风险,因为仍然可以将指针算法应用于NULL(0x0)指针,因此维基百科推荐
/* Allocate space for an array with ten elements of type int. */
int *ptr = (int*)malloc(10 * sizeof (int));
if (ptr == NULL) {
/* Memory could not be allocated, the program should handle
the error here as appropriate. */
}
Run Code Online (Sandbox Code Playgroud)
当使用sscanf时,我已经养成了将要提取的字符串分配给输入字符串大小的习惯,希望避免出现溢出的可能性.例如:
const char *inputStr = "a01234b4567c";
const …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法来查找两个字符串是否是彼此的字谜.
Ex: string1 - abcde
string2 - abced
Ans = true
Ex: string1 - abcde
string2 - abcfed
Ans = false
Run Code Online (Sandbox Code Playgroud)
我提出的解决方案是为了对两个字符串进行排序并比较两个字符串中的每个字符直到任一字符串的结尾.它将是O(logn).我正在寻找一些其他有效的方法,它不会改变比较2个字符串
C中的换行符是什么?我知道不同的操作系统有不同的行尾字符,但它们被翻译成C换行符.这个角色是什么?
我看到使用这个模式连接到我正在处理的一些代码中的字符串:
sprintf(buffer, "%s <input type='file' name='%s' />\r\n", buffer, id);
sprintf(buffer, "%s</td>", buffer);
Run Code Online (Sandbox Code Playgroud)
而且我很确定它不安全C.你会注意到buffer它既是输出又是第一个输入.
除了缓冲区溢出的明显可能性之外,我相信不能保证缓冲区在函数的开始和结束之间不会发生变化(即,无法保证缓冲区的状态在执行功能).sprintf的签名还指定了目标字符串restrict.
我还记得一篇关于memcpy中的推测性写作的报告,我认为没有理由为什么某个C库可能在sprintf中做同样的事情.当然,在这种情况下,它会写入其来源.因此,这种行为是安全的?
仅供参考,我建议:
char *bufEnd = buffer + strlen(buffer);
/* sprintf returns the number of f'd and print'd into the s */
bufEnd += sprintf(bufEnd, " <input type='file' name='%s' />\r\n", id);
Run Code Online (Sandbox Code Playgroud)
替换这个.