简体   繁体   English

Scanf字符串空间问题C

[英]Scanf String Space Issue C

Well I know many questions similar have been asked , however, I am willing to clarify if needed since my question may not be very clear. 我知道已经问过许多类似的问题,但是,由于我的问题可能不太清楚,我愿意澄清是否需要。 So it goes like this , say I want to get a space separated name string in C. Or in general I want to learn scanf why it behaves so weird for strings in case of File I/O in CI want to be good at debugging such stuff. 就像这样,说我想在C中获得一个用空格分隔的名称字符串。或者总的来说,我想学习scanf为什么它的行为如此奇特,以防CI中的文件I / O想要调试这样的东西。东西。 I should be able to print the complete string as long as it doesn't exceed my character array limit in C. I searched a lot on the topic but if it is possible to print out the internal representation of stdin in C maybe by some way. 我应该能够打印完整的字符串,只要它不超过C中的字符数组限制即可。我在该主题上进行了大量搜索,但如果可以通过某种方式打印出C中stdin的内部表示, 。 For example :- 例如 :-

#include<stdio.h>
#include<stdlib.h>
int main() {
     char name[100];    // accept 100 chars

     printf("\nEnter your name: "); // This writes to stream stdout
     scanf("%100s",name);   // Specify char limit in scanf 
     while(getchar()!='\n');   // Until user presses 'Enter' else scan 100 chars
     return 0;
}

For example, suppose I type , (space)Apple(Press Enter) I can somehow print out internal representation which is I think somewhat similar to (ASCII for space)('A''p''p''l''e''\\0')'\\n' It doesn't store '\\n' in buffer I think? 例如,假设我输入,(space)Apple(按Enter),我可以以某种方式打印出内部表示形式,我认为它有点类似于(ASCII表示空格)('A''p''p''l''e' '\\ 0')'\\ n'我认为它不将'\\ n'存储在缓冲区中吗? Now it gets a bit sophisticated to understand what happens if I give a space separated string, I know scanf discards it. 现在,如果我给出一个用空格分隔的字符串,我知道scanf会丢弃它,那么了解它会变得有些复杂。

But I fail to understand how stream stays buffered and why is it so important to understand that? 但是我不明白流如何保持缓冲,为什么理解它如此重要? No offense , I am really confused on stdin file operations & scanf in C. Maybe some good example to illustrate that. 没冒犯,我对C中的stdin文件操作和scanf真的很困惑。也许有一些很好的例子来说明这一点。 I am not a very nerdy guy so I find manuals not very beginner friendly. 我不是一个书呆子,所以我发现手册对初学者不太友好。 I am definitely interested in File Input\\Output in C :):):) 我对C的File Input \\ Output绝对感兴趣:) :) :)

scanf("%100s",name);   // Specify char limit in scanf 

specifies the maximum number of characters that can be read into name . 指定可以读入name的最大字符数。 BTW, that should be 99 since you need one element of the array for the terminating null character. 顺便说一句,应该为99,因为您需要数组的一个元素作为终止空字符。 However, this does not force reading of 100 characters. 但是,这不会强制读取100个字符。 It will still stop when a whitespace is encountered. 遇到空格时,它将仍然停止。 To read the entire line, use fgets and make sure to trim the ending newline character. 要读取整行,请使用fgets并确保修剪结尾的换行符。

fgets(name, 100, stdin); // You use 100 here. fgets will read at
                         // most 99 characters.
int len = strlen(name);
if ( name[len-1] == '\n' )
{
   name[len-1] = '\0';
}

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

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