这里是我的代码和系统调用,当只有一个单词之间没有空格或任何内容(如enter ...)时。fgets函数不读取输入中的第一个字符
例如,当我用“PWD”的号召作品,但是当我使用类似ls -l
或让我们说“CD文件1文件2”,它会清除第一个字符,并没有考虑到空间后帐户什么。
所以当我写“cd file1 file2”时只剩下“cd”的“d”。我能做些什么来防止这种情况发生?
#include <stdlib.h>
#include <stdio.h>
#include "Expert_mode.h"
void Expert_mode()
{
printf("Your are now in Expert mode, You are using a basic Shell (good luck) \nWe added the commands 'read_history', 'leave' and 'Easter_egg'. \n");
int a = 0;
while(a == 0)
{
char* line;
getchar();
printf("Choose a command : \n");
line = malloc(100*sizeof(char));
fgets(line, 100, stdin);
if(strcoll(line, "leave") == 0)
{
a = 1;
}
else if(strcoll(line, "read_history") == 0)
{
//read_history();
}
else if(strcoll(line, "Easter_egg") == 0)
{
// Easter_egg();
}
else
{
system(line);
}
}
}
如果你要分配一个固定大小的缓冲区,为什么不使用数组?那么你将不会有内存泄漏。 –