Usage of fgets function in C

后端 未结 7 1615
野性不改
野性不改 2020-12-19 17:51

One of my assignments in to write my own UNIX Shell. To receive input from the user, I am using fgets to capture the input as a string but I\'m not really sure how it works.

7条回答
  •  眼角桃花
    2020-12-19 18:33

    fgets is capturing the line break, too.

    Note that you can overcome this in a few ways, one might be using strncmp:

    if((strncmp(command, "exit", 4)) == 0)
    

    which checks if only the first 4 characters of command match (though this might not be the right option for you here).

    Another tactic is to check with the line break in place:

    if((strcmp(command, "exit\n")) == 0)
    

提交回复
热议问题