c - 我的 C 程序因未知原因而崩溃
问题描述
所以我的程序本来是一个小命令行,但它一直在崩溃:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void main()
{
char cmd;
for(;;)
{
fgets(cmd,255,stdin);
if (strstr(cmd,"CD")!=NULL )
{
cmd +=2;
SetCurrentDirectory(cmd);
}
else
{
system(cmd);
}
}
}
编译器输出是预期的左值。
解决方案
也许你正在寻找做这样的事情:
void main()
{
char cmd[255]; // this allocate a character array to store the command in
for(;;)
{
fgets(cmd,255,stdin); // get the characters from stdin and store them in the cmd character array with a max length of 255
if ( strncmp(cmd, "CD ", 3) == 0 ) // check if the first three characters are "CD "
{
SetCurrentDirectory(&cmd[3]); // pass the string not including the first 3 charcters, which should be "CD "
}
else
{
system(cmd);
}
}
}
推荐阅读
- php - 无脂肪模板路由中的动态参数
- amazon-web-services - 上一步功能完成后运行 cloudwatch 规则
- netsuite - NetSuite SuiteTalk 项目履行错误 - VALID_LINE_ITEM_REQD
- node.js - 当我尝试使用 bodyparser 在控制台上查看我的表单内容时,它不会显示我的表单内容
- python - 如何从python中的字符串中删除多个随机字符
- mfc - 如何检测 CEdit 控件的文本对齐方式?
- android - 我无法将“代码”作为问题的一部分包含在该系统中。我放入“身体”的任何东西
- typescript - 为什么在泛型上使用 InstanceType 是错误的
- list - 删除单链表中的节点
- opengl - 正射投影导致几何被远平面剪裁