If I execute the exec() function in another C program as a main function it works perfectly, while if I put it as a function called in the main menu it gives me some warning and the function does not run.
如果我在另一个C程序中执行exec()函数作为一个主函数,它的工作非常完美,而如果我把它作为一个函数在主菜单中调用,它会给我一些警告,函数不会运行。
My code:
我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /* for fork */
#include <sys/types.h> /* for pid_t */
#include <sys/wait.h> /* for wait */
int exec (void) {
char array[100];
char character;
int i = 0;
char* point;
int j = 0;
printf ("Digita una stringa");
printf ("\n");
do {
character = getchar();
array[i] = character;
i++;
}
while (character != '\n');
array[i-1] = '\0';
i = 0;
char* string[100];
char *word = strtok(array, " .");
j = 0;
while (word != NULL) {
printf("%s\n", word);
string[j++] = word; // Increment j
word = strtok(NULL, " .");
}
string[j] = NULL; // Make sure the array is NULL term
printf ("\n");
pid_t pid;
pid = fork();
int status;
if (pid == -1) {
perror("");
}else if (pid == 0) {
execvp(string[0], string); /* execute the command */
fprintf(stderr, "Failed to exec");
exit(1);
}
else {
//.. wait until the child ends
waitpid(-1, &status, 0);
}
return;
}
int read_input (void) {
int choice;
printf("Seleziona una voce dal menu");
do {
printf("\n");
scanf ("%i", &choice);
if (choice > 8 || choice < 1)
printf ("Attenzione, inserisci un valore contemplato dal menu");
}
while ( choice > 8 || choice < 1);
return choice;
}
void main (int argc, char *argv[]) {
printf ("------------------------\n");
printf (" MENU \n");
printf ("------------------------\n");
printf (" \n");
printf ("1) Esecuzione in foreground di un processo\n");
printf ("2) Ctrl -C\n");
printf ("3) Exit\n");
printf ("4) Background\n");
printf ("5) Pipe\n");
printf ("6) Jobs\n");
printf ("7) fg\n");
printf ("8) kill\n");
int menu = read_input();
switch (menu) {
case '1' :
exec ();
break;
case '2' :
//ctrl();
break;
case '3' :
//exit_();
break;
case '4' :
//background();
break;
case '5' :
//pipe();
break;
case '6' :
//jobs();
break;
case '7' :
//fg();
break;
case '8' :
//kill();
break;
}
}
this is the warning:
这是一个警告:
elaborato.c:31:16: warning: initialization makes pointer from integer without a cast [enabled by default] char *word = strtok(array, " .");
4 个解决方案
#1
2
Regarding the problem related to input,
关于输入的问题,
do {
printf("\n");
scanf ("%i", &choice);
if (choice > 8 || choice < 1)
printf ("Attenzione, inserisci un valore contemplato dal menu");
}
while ( choice > 8 || choice < 1);
Once you type an integer and press enter, the scanf()
consumes the number and a newline is left in stdin. Next time the loop goes around (assuming input <1 or >8 or something else) scanf gets that newline and it goes on. add a getchar()
after the scanf()
.
一旦键入一个整数并按enter, scanf()就会消耗这个数字,而在stdin中会留下一条新行。下一次循环的时候(假设输入<1或>8或其他东西),scanf会得到这个换行,然后继续。在scanf()之后添加一个getchar()。
#2
1
The answer is in the warnings, you should move them from the comment into the question.
答案是在警告中,你应该把他们从评论中移到问题中。
elaborato.c:31:16: warning: initialization makes pointer from integer without a cast [enabled by default] char *word = strtok(array, " .");
That means that word
, which is a char
pointer is being initialized from an integer. Therefore, it seems strtok()
is returning an integer... that doesn't sound right.
也就是说,一个char指针是由一个整数初始化的。因此,strtok()似乎返回一个整数…这听起来不正确的。
From the strtok()
man page:
从strtok()手册页:
#include <string.h>
char *strtok(char *str, const char *delim);
char *strtok_r(char *str, const char *delim, char **saveptr);
That seems right, it returns a char *
.... but it also says it's declared in <string.h>
... which you aren't including. Since it's not defined, the compiler assumes it as int strtok()
.
这似乎是正确的,它返回一个char * ....但是它也说它在
Fix: add the #include <string.h>
line.
修复:添加#include
#3
0
The problem you are seeing is that of scanf
getting skipped.
你看到的问题是scanf被跳过。
For more details you can refer here
更多细节,请点击这里。
The call to scanf()
after the printf
consumes the new-line character and continues without the user having to enter anything.
在printf之后调用scanf()会消耗新行字符,并且在不需要用户输入任何内容的情况下继续。
It reads the very next character from standard in, which is probably a newline character and thus not prompting you for any input.
它从标准中读取下一个字符,它可能是一个换行符,因此不会提示您输入任何信息。
#4
0
I'm too tired to comment on the entire code (it is really bad), so I'll just write about the issue in question and how trivially it could be debugged.
我太累了,无法对整个代码进行评论(这真的很糟糕),所以我将只写关于这个问题的内容,以及它是如何被调试的。
First thing to do is to check if we get the number and return to main, hence:
首先要做的是检查我们是否得到这个号码,然后返回main,因此:
int menu = read_input();
printf("got [%d]\n", menu);
Running this:
运行:
[snip]
1
got [1]
So we indeed get to this point.
所以我们到了这一点。
As such now we check what is this compared to.
现在我们来看看这个和什么相比。
int menu = read_input();
printf("got [%d] '1'=[%d]\n", menu, '1');
Let's run:
让我们运行:
1
got [1] '1'=[49]
So, menu stores an integer 1, while '1' is a character code for 1 in ascii, which, well, is not an integer 1.
因此,菜单存储一个整数1,而“1”则是ascii中的一个字符代码,这不是整数1。
In general I don't see what was the problem with narrowing it down.
总的来说,我不认为缩小范围有什么问题。
#1
2
Regarding the problem related to input,
关于输入的问题,
do {
printf("\n");
scanf ("%i", &choice);
if (choice > 8 || choice < 1)
printf ("Attenzione, inserisci un valore contemplato dal menu");
}
while ( choice > 8 || choice < 1);
Once you type an integer and press enter, the scanf()
consumes the number and a newline is left in stdin. Next time the loop goes around (assuming input <1 or >8 or something else) scanf gets that newline and it goes on. add a getchar()
after the scanf()
.
一旦键入一个整数并按enter, scanf()就会消耗这个数字,而在stdin中会留下一条新行。下一次循环的时候(假设输入<1或>8或其他东西),scanf会得到这个换行,然后继续。在scanf()之后添加一个getchar()。
#2
1
The answer is in the warnings, you should move them from the comment into the question.
答案是在警告中,你应该把他们从评论中移到问题中。
elaborato.c:31:16: warning: initialization makes pointer from integer without a cast [enabled by default] char *word = strtok(array, " .");
That means that word
, which is a char
pointer is being initialized from an integer. Therefore, it seems strtok()
is returning an integer... that doesn't sound right.
也就是说,一个char指针是由一个整数初始化的。因此,strtok()似乎返回一个整数…这听起来不正确的。
From the strtok()
man page:
从strtok()手册页:
#include <string.h>
char *strtok(char *str, const char *delim);
char *strtok_r(char *str, const char *delim, char **saveptr);
That seems right, it returns a char *
.... but it also says it's declared in <string.h>
... which you aren't including. Since it's not defined, the compiler assumes it as int strtok()
.
这似乎是正确的,它返回一个char * ....但是它也说它在
Fix: add the #include <string.h>
line.
修复:添加#include
#3
0
The problem you are seeing is that of scanf
getting skipped.
你看到的问题是scanf被跳过。
For more details you can refer here
更多细节,请点击这里。
The call to scanf()
after the printf
consumes the new-line character and continues without the user having to enter anything.
在printf之后调用scanf()会消耗新行字符,并且在不需要用户输入任何内容的情况下继续。
It reads the very next character from standard in, which is probably a newline character and thus not prompting you for any input.
它从标准中读取下一个字符,它可能是一个换行符,因此不会提示您输入任何信息。
#4
0
I'm too tired to comment on the entire code (it is really bad), so I'll just write about the issue in question and how trivially it could be debugged.
我太累了,无法对整个代码进行评论(这真的很糟糕),所以我将只写关于这个问题的内容,以及它是如何被调试的。
First thing to do is to check if we get the number and return to main, hence:
首先要做的是检查我们是否得到这个号码,然后返回main,因此:
int menu = read_input();
printf("got [%d]\n", menu);
Running this:
运行:
[snip]
1
got [1]
So we indeed get to this point.
所以我们到了这一点。
As such now we check what is this compared to.
现在我们来看看这个和什么相比。
int menu = read_input();
printf("got [%d] '1'=[%d]\n", menu, '1');
Let's run:
让我们运行:
1
got [1] '1'=[49]
So, menu stores an integer 1, while '1' is a character code for 1 in ascii, which, well, is not an integer 1.
因此,菜单存储一个整数1,而“1”则是ascii中的一个字符代码,这不是整数1。
In general I don't see what was the problem with narrowing it down.
总的来说,我不认为缩小范围有什么问题。