如何将输入响应传递给C中的函数
我试着问一个用户,他们想在我制作的“外壳”中做什么。我的代码如下:如何将输入响应传递给C中的函数,c,arrays,pointers,C,Arrays,Pointers,我试着问一个用户,他们想在我制作的“外壳”中做什么。我的代码如下: /* Create a char array to hold response */ char response[80]; char exit[4] = "Exit"; printf("\n\n Commands are as followed:"); printf("\n------------------------------------"); printf("\n'cd' 'Directory' (Ch
/* Create a char array to hold response */
char response[80];
char exit[4] = "Exit";
printf("\n\n Commands are as followed:");
printf("\n------------------------------------");
printf("\n'cd' 'Directory' (Changes Directorys)");
printf("\n'cp' 'FileName' (Copys File)");
printf("\n'Exit' (Exits the program)");
printf("\n'ls' (Displays Info)");
printf("\n------------------------------------\n\n");
while(strcmp(exit, response) != 0){
/* Ask user for input */
fputs("$> ", stdout);
/* Flush */
fflush(stdout);
/* Make sure response is not NULL */
if ( fgets(response, sizeof response, stdin) != NULL )
{
/* search for newline character */
char *newline = strchr(response, '\n');
if ( newline != NULL )
{
/* overwrite trailing newline */
*newline = '\0';
}
actOnResponse(fs, response);
}
现在我的问题是关于actOnResponse()函数。我想将响应传递给函数。在函数中,我将解析响应并将其与值“cd”、“cp”和“ls”进行比较。但我如何传递它呢?现在,当我运行程序时,它会给我一个分段错误(核心转储)
任何能指出我做错了什么的人都会很好!多谢各位
以下是ActInResponse()函数:
void actOnResponse(int fs, char *response){
printf("%s" , response);
}
#include <stdio.h>
#include <string.h>
void actOnResponse(int fs, char *response){
printf("%s\n" , response);
}
void main() {
/* Create a char array to hold response */
char response[80];
char exit[] = "Exit";
int fs = 0;
response[0] = 0;
printf("\n\n Commands are as followed:");
printf("\n------------------------------------");
printf("\n'cd' 'Directory' (Changes Directorys)");
printf("\n'cp' 'FileName' (Copys File)");
printf("\n'Exit' (Exits the program)");
printf("\n'ls' (Displays Info)");
printf("\n------------------------------------\n\n");
while (strcmp(exit, response) != 0) {
/* Ask user for input */
fputs("$> ", stdout);
/* Flush */
fflush(stdout);
/* Make sure response is not NULL */
if (fgets(response, sizeof response, stdin) != NULL) {
/* search for newline character */
char *newline = strchr(response, '\n');
if (newline != NULL) {
/* overwrite trailing newline */
*newline = '\0';
}
actOnResponse(fs, response);
}
}
}
#包括
#包括
void actionresponse(int fs,char*response){
printf(“%s\n”,响应);
}
void main(){
/*创建一个字符数组来保存响应*/
字符响应[80];
char exit[]=“退出”;
int fs=0;
响应[0]=0;
printf(“\n\n命令如下:”);
printf(“\n--------------------------------------------------------”;
printf(“\n'cd”“目录(更改目录)”);
printf(“\n'cp”“文件名”(复制文件)”;
printf(“\n'Exit'(退出程序)”);
printf(“\n'ls”(显示信息)”);
printf(“\n--------------------------------------\n\n”);
while(strcmp(退出,响应)!=0){
/*请求用户输入*/
FPUT(“$>”,标准输出);
/*冲洗*/
fflush(stdout);
/*确保响应不为NULL*/
if(fgets(响应,响应大小,标准输入)!=NULL){
/*搜索换行符*/
char*newline=strchr(响应'\n');
if(换行符!=NULL){
/*覆盖尾随换行符*/
*换行符='\0';
}
行动响应(fs,响应);
}
}
}
(另存为test.c
.rangcc-o test test.o
然后/test
)
#include <stdio.h>
#include <string.h>
void actOnResponse(int fs, char *response){
printf("%s\n" , response);
}
void main() {
/* Create a char array to hold response */
char response[80];
char exit[] = "Exit";
int fs = 0;
response[0] = 0;
printf("\n\n Commands are as followed:");
printf("\n------------------------------------");
printf("\n'cd' 'Directory' (Changes Directorys)");
printf("\n'cp' 'FileName' (Copys File)");
printf("\n'Exit' (Exits the program)");
printf("\n'ls' (Displays Info)");
printf("\n------------------------------------\n\n");
while (strcmp(exit, response) != 0) {
/* Ask user for input */
fputs("$> ", stdout);
/* Flush */
fflush(stdout);
/* Make sure response is not NULL */
if (fgets(response, sizeof response, stdin) != NULL) {
/* search for newline character */
char *newline = strchr(response, '\n');
if (newline != NULL) {
/* overwrite trailing newline */
*newline = '\0';
}
actOnResponse(fs, response);
}
}
}
#包括
#包括
void actionresponse(int fs,char*response){
printf(“%s\n”,响应);
}
void main(){
/*创建一个字符数组来保存响应*/
字符响应[80];
char exit[]=“退出”;
int fs=0;
响应[0]=0;
printf(“\n\n命令如下:”);
printf(“\n--------------------------------------------------------”;
printf(“\n'cd”“目录(更改目录)”);
printf(“\n'cp”“文件名”(复制文件)”;
printf(“\n'Exit'(退出程序)”);
printf(“\n'ls”(显示信息)”);
printf(“\n--------------------------------------\n\n”);
while(strcmp(退出,响应)!=0){
/*请求用户输入*/
FPUT(“$>”,标准输出);
/*冲洗*/
fflush(stdout);
/*确保响应不为NULL*/
if(fgets(响应,响应大小,标准输入)!=NULL){
/*搜索换行符*/
char*newline=strchr(响应'\n');
if(换行符!=NULL){
/*覆盖尾随换行符*/
*换行符='\0';
}
行动响应(fs,响应);
}
}
}
(另存为test.c
.rangcc-o test test.o
然后/test
)
#include <stdio.h>
#include <string.h>
void actOnResponse(int fs, char *response){
printf("%s\n" , response);
}
void main() {
/* Create a char array to hold response */
char response[80];
char exit[] = "Exit";
int fs = 0;
response[0] = 0;
printf("\n\n Commands are as followed:");
printf("\n------------------------------------");
printf("\n'cd' 'Directory' (Changes Directorys)");
printf("\n'cp' 'FileName' (Copys File)");
printf("\n'Exit' (Exits the program)");
printf("\n'ls' (Displays Info)");
printf("\n------------------------------------\n\n");
while (strcmp(exit, response) != 0) {
/* Ask user for input */
fputs("$> ", stdout);
/* Flush */
fflush(stdout);
/* Make sure response is not NULL */
if (fgets(response, sizeof response, stdin) != NULL) {
/* search for newline character */
char *newline = strchr(response, '\n');
if (newline != NULL) {
/* overwrite trailing newline */
*newline = '\0';
}
actOnResponse(fs, response);
}
}
}
#包括
#包括
void actionresponse(int fs,char*response){
printf(“%s\n”,响应);
}
void main(){
/*创建一个字符数组来保存响应*/
字符响应[80];
char exit[]=“退出”;
int fs=0;
响应[0]=0;
printf(“\n\n命令如下:”);
printf(“\n--------------------------------------------------------”;
printf(“\n'cd”“目录(更改目录)”);
printf(“\n'cp”“文件名”(复制文件)”;
printf(“\n'Exit'(退出程序)”);
printf(“\n'ls”(显示信息)”);
printf(“\n--------------------------------------\n\n”);
while(strcmp(退出,响应)!=0){
/*请求用户输入*/
FPUT(“$>”,标准输出);
/*冲洗*/
fflush(stdout);
/*确保响应不为NULL*/
if(fgets(响应,响应大小,标准输入)!=NULL){
/*搜索换行符*/
char*newline=strchr(响应'\n');
if(换行符!=NULL){
/*覆盖尾随换行符*/
*换行符='\0';
}
行动响应(fs,响应);
}
}
}
(另存为test.c
.rangcc-o test test.o
然后/test
)
#include <stdio.h>
#include <string.h>
void actOnResponse(int fs, char *response){
printf("%s\n" , response);
}
void main() {
/* Create a char array to hold response */
char response[80];
char exit[] = "Exit";
int fs = 0;
response[0] = 0;
printf("\n\n Commands are as followed:");
printf("\n------------------------------------");
printf("\n'cd' 'Directory' (Changes Directorys)");
printf("\n'cp' 'FileName' (Copys File)");
printf("\n'Exit' (Exits the program)");
printf("\n'ls' (Displays Info)");
printf("\n------------------------------------\n\n");
while (strcmp(exit, response) != 0) {
/* Ask user for input */
fputs("$> ", stdout);
/* Flush */
fflush(stdout);
/* Make sure response is not NULL */
if (fgets(response, sizeof response, stdin) != NULL) {
/* search for newline character */
char *newline = strchr(response, '\n');
if (newline != NULL) {
/* overwrite trailing newline */
*newline = '\0';
}
actOnResponse(fs, response);
}
}
}
#包括
#包括
void actionresponse(int fs,char*response){
printf(“%s\n”,响应);
}
void main(){
/*创建一个字符数组来保存响应*/
字符响应[80];
char exit[]=“退出”;
int fs=0;
响应[0]=0;
printf(“\n\n命令如下:”);
printf(“\n--------------------------------------------------------”;
printf(“\n'cd”“目录(更改目录)”);
printf(“\n'cp”“文件名”(复制文件)”;
printf(“\n'Exit'(退出程序)”);
printf(“\n'ls”(显示信息)”);
printf(“\n--------------------------------------\n\n”);
while(strcmp(退出,响应)!=0){
/*请求用户输入*/
FPUT(“$>”,标准输出);
/*冲洗*/
fflush(stdout);
/*确保响应不为NULL*/
if(fgets(响应,响应大小,标准输入)!=NULL){
/*搜索换行符*/
char*newline=strchr(响应'\n');
if(换行符!=NULL){
/*覆盖尾随换行符*/
*换行符='\0';