fgets() includes the newline at the end [duplicate]

帅比萌擦擦* 提交于 2019-11-26 09:14:18

问题


This question already has an answer here:

  • Removing trailing newline character from fgets() input 12 answers
fgets(input,sizeof(input),stdin);
if (strcmp(input, \"quit\") == 0){
  exit(-1);
}

If I type quit, it does not exit the program; I\'m wondering why this is the case.

By the way input is declared as char *input;.


回答1:


Trailing newline in your input. See man fgets. Test for "quit" + newline, for example:

fgets(input,sizeof(input),stdin);
if(strcmp(input, "quit\n") == 0){
    exit(-1);
}

I completely missed the last sentence, re char *input. Depending on the architecture, input will be 4 or 8 bytes long. So the code is effectively

fgets(input, 8, stdin);

which doesn't reflect the real size of memory, input points to. This might "work" as long as the input is shorter than eight bytes, but will truncate the input, if it is larger. Furthermore, you will get the rest of the input the next time you call fgets.

You should either give the real size or take @JonathanLeffler's advice and declare a char array instead, e.g.

char input[64];
fgets(input, sizeof(input), stdin);

or

char *input = malloc(N);
fgets(input, N, stdin);



回答2:


The function fgets might add a newline at the end of the string read. You'll have to check that:

size_t ln = strlen(input) - 1;
if (input[ln] == '\n')
    input[ln] = '\0';

or even

strtok(input, "\n");



回答3:


Suggest you code this as:

if(strstr(input, "quit") != NULL){

Reason: This will solve issue of people adding extra characters (e.g. space before or after text).




回答4:


This solution only needs the standard library (stdio.h) and gives the same results.

for (i = 0; input[i] != '\0'; i++); /* getting the string size */
input[i-1] = '\0'; /* removing the newline */



回答5:


what i did is to replace newline by '\0' null .

while(fgets(message,80,stdin))
{
    l=strlen(message)-1;
    if(message[l]='\n') message[l]='\0';
            else message[i+1]='\0';
}


来源:https://stackoverflow.com/questions/13443793/fgets-includes-the-newline-at-the-end

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!