C语言 扫描到新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27677063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:39:33  来源:igfitidea点击:

Scanning until new line

cscanf

提问by Aditya Kiran

I want to read all the text entered until a new line character is entered.

我想阅读所有输入的文本,直到输入新的行字符。

This is my code.

这是我的代码。

int i=0;
char ch[MAX];
printf("Enter the text\n");
while(true)
{
     scanf("%c",&ch[i]);
     if(ch[i]=='\n')
         break;
     i++;
}

But when I try to execute it reads only one word.

但是当我尝试执行它时,它只读取一个字。

I have also tried scanf("%s",ch);but the result is the same.

我也试过,scanf("%s",ch);但结果是一样的。

回答by unwind

You're not checking that scanf()succeeds before relying on ch[i]to have a valid value, that's not a good idea.

scanf()在依赖ch[i]有效值之前,您没有检查是否成功,这不是一个好主意。

Just use fgets()to read a whole line at once.

只需用于fgets()一次读取整行。

回答by Spikatrix

Transferring comment to answer.

转发评论回答。

Your code will work. The code you posted scans everything until a newline character(\n) is found. But as Jonathan Lefflercommented, you never NUL-terminate your string. To do it just use

您的代码将起作用。您发布的代码会扫描所有内容,直到\n找到换行符 ( )。但正如Jonathan Leffler评论的那样,您永远不会以 NUL 结尾您的字符串。要做到这一点,只需使用

ch[i]='
if(i==MAX-1)
break;
';

after the loop. Also, the user could enter more characters than MAX-1(One extra for the \0at the end) and this could cause a buffer overflow. You should add a check like

循环后。此外,用户可以输入比MAX-1(最后一个额外的字符)更多的字符\0,这可能会导致缓冲区溢出。你应该添加一个像

scanf("%[^\n]",ch);
getchar();

just before your scanfin order to prevent it from overflowing.

就在你之前scanf,以防止它溢出。

Note that scanf("%s",ch);will scan until it encounters a space or a newline character.

请注意,scanf("%s",ch);它将扫描直到遇到空格或换行符。



而不是逐字符循环和扫描,只需使用

scanf("%49[^\n]",ch);

The above scanfscans everything until a newline character is found and puts them in ch. The getchar()then discards the \nfrom the stdin. You can also improve safety by limiting the amount of characters that scanfreads into ch.

以上scanf扫描所有内容,直到找到换行符并将它们放入ch. 在getchar()随后丢弃\nstdin。您还可以通过限制scanf读入的字符数量来提高安全性ch

int i=0;
char ch[MAX];
int single;  // Important that this in an int to distinguish EOF from input.

printf("Enter the text\n");

while((single = fgetc(stdin)) != EOF) {
  if (i >= (MAX-1)) {
    ;  // Too many, do not save or maybe indicate error
  } else {
    ch[i++] = single;
  }
  if (single == '\n') {
    break;
  }
}
ch[i] = '
int main()
{
    int i=0;
    char ch[100];
    printf("Enter the text\n");
    gets(ch);  // input text
    puts(ch);  // output text
    return 0;
}
'; // Add termination

The above scanfwill scan a maximum of 49 characters and will add a \0at the end. You can substitute the value of MAX-1there. I've used 50 as an example.

以上scanf将扫描最多 49 个字符,并\0在末尾添加一个。您可以替换MAX-1那里的值。我以 50 为例。

回答by chux - Reinstate Monica

As commented by @Jonathan Leffler, OP 's code does not null terminate the string or prevent buffer overflow.

正如@Jonathan Leffler 所评论的那样, OP 的代码不会空终止字符串或防止缓冲区溢出。

Since code fetches 1 charat a time, use the much simpler fgetc().

由于代码一次获取 1 char,因此使用更简单的fgetc().

##代码##

回答by Bond

your code working fine . I checked, it reads a line not a word.

你的代码工作正常。我查了一下,它读的是一行而不是一个字。

回答by Sakib Ahammed

I hope it will be better for you with respect to your code :

我希望它对你的代码会更好:

##代码##

input : asdf ghjkl zxcvb

输入 : asdf ghjkl zxcvb

output: asdf ghjkl zxcvb

输出: asdf ghjkl zxcvb