简体   繁体   English

C:fgets()和命令行输入

[英]C: fgets() and command line input

I'm trying to write a program that handles processes. 我正在尝试编写一个处理过程的程序。 The user will be prompted.. 将提示用户。

shell: start emacs file.c
Process 346 has started.

I'm having a few issues with fgets, since I don't know what to put as my third argument (I want it to take in from the command line/argv) with 我在使用fgets时遇到了一些问题,因为我不知道在第三个参数(我希望它从命令行/ argv接受)中添加什么

fgets(buffer, 4096, How can I do a file pointer if I'm not doing a file, just argv?) fgets(缓冲区,4096,如果我不做文件, 只是argv ,我该如何做文件指针?)

int main(int argc, char **argv)   
{
 while(1) {
    printf("shell: ");   
int status, pid;
int i=0;
char buffer[4097];
    fgets(buffer, 4096, ???? ) //I want it to take in from argv. Do I need to declare 4096?

if(strcmp(argv[1],"start") == 0 )
    { printf("myshell: process has started \n");
           while (str = strtok(buffer," ") = NULL)  //??? What do I declare str as?
            argv[i] = str;    
            i = i+1;
            }      

            if ((pid = fork()) < 0) {  //Child process fork
            perror("fork");       
            exit(1);
            }
            if (pid == 0) {  //Child executes code
            execvp(argv[0], argv );
            perror(argv[1]);    //argv1? 
            exit(1);
             }
    }

Does this make sense fort the desired behavior explained above? 这对上面解释的期望行为有意义吗? I'm not entirely sure of the location of my while loop that has strtok, didn't know if that maybe has to be outside of the if-statement. 我不确定strtok的while循环的位置,也不知道是否可能必须在if语句之外。

Also, is it correct for my strtok function to be called as strtok(buffer, " ")? 另外,将我的strtok函数称为strtok(buffer,“”)是否正确? Thank you. 谢谢。

 I don't know what to put as my third argument ??

From the man page, 在手册页中
char *fgets(char * restrict s, int n, FILE * restrict stream);

So third argument is FILE pointer. 因此,第三个参数是FILE指针。 Since argv is pointer to string, you can't use in fgets() as mentioned. 由于argv是指向字符串的指针,因此您不能在fgets()中使用它。

you can directly use or copy to some local buffer using strcpy() family of functions. 您可以使用strcpy()系列函数直接使用或复制到某些本地缓冲区。

You are going wrong entirely my friend when you will start program through terminal (./your_program argv1 argv2 ... argvN) argv is passed by system to the main function, but after starting the program nothing will going store in argv ie 当您通过终端启动程序时,您完全错了我的朋友(./your_program argv1 argv2 ... argvN)argv由系统传递给主函数,但是启动程序后,所有内容都不会存储在argv中,即

./your_program argv1 argv2 //here you can collect string input from system shell: start emac file.c //this will not stored into argv ./your_program argv1 argv2 //在这里您可以从系统外壳收集字符串输入:start emac file.c //这不会存储到argv中

to take value those value you can something do like that fgets(buffer, 4096,stdin) // from console into your buffer not in argv 要获取这些值,您可以执行以下操作:fgets(buffer,4096,stdin)//从控制台进入缓冲区,而不是在argv中

and after that you can use string tokeniser function to take each value from buffer I do not know exact syntax or program for string tokeniser but this link will help you http://www.cplusplus.com/reference/cstring/strtok/ 然后,您可以使用字符串标记处理程序函数从缓冲区中获取每个值,我不知道字符串标记处理程序的确切语法或程序,但是此链接将为您提供帮助http://www.cplusplus.com/reference/cstring/strtok/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM