简体   繁体   English

Scanf(“%[^ \\ n]”)不在循环内使用数组字符串

[英]Scanf(“%[^\n]”) not working with array string inside loop

I've written this code in C. I need to solve a problem and there I'll have to input 5 line string including whitespace. 我用C编写了这段代码。我需要解决一个问题,我必须输入5行字符串,包括空格。 This program will just give me output of all 5 line string including whitespace. 这个程序只给我输出所有5行字符串,包括空格。 By white space I mean on input I can put space before and char or after any char. 通过空格我的意思是在输入中我可以放置空格之前和char或任何char之后。 That's why I've written this code but I can't understand why it's not working. 这就是我编写这段代码的原因,但我无法理解为什么它不起作用。

#include<stdio.h>
int main() {
    char str[5][100];
    for(int i=0;i<5;i++) {
        scanf("%[^n\]",str[i]);
    }
    for(int j=0;j<5;j++) {
        printf("%s\n",str[j]);
    }
    return 0;
}

I tried to use only 我试过只使用

scanf("%s",str[i]);

but then it's ignoring all whitespace inside the input and trimming the output. 但是它忽略了输入中的所有空格并修剪输出。 Also I tried to use 我也试着用

scanf(" %[^\n]",str[i]);

this time little better but it's ignoring the all white space before any character a example input is like. 这一次好一点,但是在示例输入之类的任何字符之前忽略了所有空白区域。

    Robin Islam
// output showing
Robin Islam
// should show
    Robin Islam

I just want to make this program to allow whitespace on every I mean output should show the same as input without ignoring space. 我只是想让这个程序允许每个我的意思输出上的空格应该显示与输入相同而不忽略空格。 Someone please help me. 有人请帮帮我。 Tried lot's of way but don't know how to make it works or how......Help please 尝试了许多方法,但不知道如何使它工作或如何......请帮助

Thanks, Robin 谢谢,罗宾

scanf is riddled with problems, just search for it here and you'll see. scanf充满了问题,只需在这里搜索它 ,你就会看到。 It should be avoided whenever possible. 应尽可能避免这种情况。

You're reading whole lines, and there are functions for doing that. 你正在阅读整行,并且有这样做的功能。 fgets and getline . fgetsgetline I prefer getline because it handles memory allocation for you, there's no risk of your input overrunning a buffer. 我更喜欢getline因为它为你处理内存分配,你的输入没有超出缓冲区的风险。

#include <stdio.h>
#include <strings.h>
#include <stdlib.h>

int main() {
    char *line = NULL;
    char *lines[5];
    size_t linecap = 0;

    for( int i = 0; i < 5; i++ ) {
        /* Getline will allocate sufficient memory to line.
           It will also reuse line, so... */
        getline(&line, &linecap, stdin);

        /* ...we have to copy the line */
        lines[i] = strdup(line);
    }

    /* line must be freed after calls to getline() are finished */
    free(line);

    for( int i = 0; i < 5; i++ ) {
        printf("%s\n", lines[i]);
    }

    /* Cleaning up all memory is a good habit to get into.
       And it removes clutter from you Valgrind report. */
    for( int i = 0; i < 5; i++ ) {
        free(lines[i]);
    }

    return 0;
}
#include<stdio.h>
#include<stdlib.h>

int main() {

    char str[5][100];
    for(int i=0;i<5;i++) {
        fgets(str[i],100,stdin);
    }
    for(int j=0;j<5;j++) {
        printf("%s\n",str[j]);
    }

    return 0;
}

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

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