#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char string;
printf("Hello\n");
printf("What would you like to do\n");
printf("Here are the options\n");
printf("s : How are you\n");
printf("c : What would you like to search\n");
scanf("%s",&string);
if(string == 'h')
printf("iam fine\n");
else if (string == 's')
printf("What would you like to search\n");
scanf("%s",&string);
system(string);
return 0;
}
当我在它说你想搜索什么后运行它并且我输入 run notepad
它停止工作。
最佳答案
此 scanf 有两个问题:
printf("What would you like to search\n");
scanf("%s",&string);
system(string);
string
是单个字符 -scanf
将导致缓冲区溢出。%s
格式说明符仅读取到下一个空格。
要解决此问题,您应该分配更大的缓冲区并读取整行:
char buffer[1024];
printf("What would you like to search\n");
fgets(buffer, sizeof buffer, stdin);
system(buffer);
关于CodeBlocks exe 停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28943987/