9
回答
c 求救啊,exit 就自动挂掉了
华为云实践训练营,热门技术免费实践!>>>   

为什么我输入exit 程序就挂掉了 ?


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>

struct complex_struct {
	
	static void exit(char *s) {
		if(strcmp(s, "exit")) return ;
		exit(0);
	}
	
	static void ls(char *s) {
		
		if(strcmp(s, "ls")) return ;
		
		struct _finddata_t files;
		int fhandle;
		int i = 0;
		
		fhandle = _findfirst("d:/*", &files);
		if(fhandle == -1) {
			printf("ls: cannot access /: No such file or directory\n");
			return ;
		}
		do{	
			if(!(i%3)) {
				printf("%s\n", files.name);
			}else{
				printf("%s\t", files.name);
			}
			i++;	
		}
		while(_findnext(fhandle, &files) == 0);
		printf("\n");
		_findclose(fhandle);
	}
	
	static void clear(char *s) {
		if(strcmp(s, "clear")) return ;
		system("cls");
	}
	
} command;


int main() {
	char *s;
	char *prefix = "[root@localhost /]# ";
	printf("%s", prefix);
	while(true) {
		gets(s);
		command.exit(s);
		command.ls(s);
		command.clear(s);
		printf("%s", prefix);
	}	
	return 0;
}


举报
好为
发帖于4年前 9回/601阅
顶部