fgets问题 [英] fgets question

查看:55
本文介绍了fgets问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正和某人谈论fgets,他说fgets将

\ n放在一个字符串中但不是\0。我决定测试这个假设,因为我的

文档没有说fgets是否在字符串文字之后放置了\0。 Strlen是

我以前决定没有添加\0。如何为

字符串添加它?我的代码。


int main(void){

char输入[10];

fgets(输入,sizeof(输入) ),stdin);

printf("%i \ n",strlen(输入));

}


比printf报告的字符多一个字符。


比尔

解决方案

" ;比尔坎宁安 < no **** @ nspam.comwrites:


我正和某人谈论fgets,他说fgets把

字符串中的\ n但不是\0。



你在哪里见到人们聊聊C?


我决定测试这个假设,因为我的

文档没有说fgets是否在字符串文字后放置\0。 Strlen是

我以前决定没有添加\0。



是什么让你这么想的?必须有一个空字节或strlen不会

工作。


怎样才能添加一个

字符串?



它已经存在。如果fgets耗尽了空间,它将停止一个简短的
,这样它总是可以将终止空值放在缓冲区中。你

可能得不到''\ n'',但你总会得到0.


我的代码。


int main(void){

char输入[10];

fgets(输入,sizeof(输入),stdin);

printf("%i \ n",strlen(input));



更好的是:

printf("%i \ n",(int)strlen(输入));


因为%i需要一个int而不是size_t。或者,使用%zu。作为

格式,因为你似乎正在使用接近C99的东西。


}


比printf报告的字符多一个字符。



''\ n''是一个字符。如果你输入abc并点击返回,fgets将五个东西放入输入数组:''a'','b'',''c'',''\ n''和''\'''。这个

是一个四字符串和strlen报告4.


-

Ben。


2008年6月9日星期一01:54:12 GMT,Bill Cunningham < no **** @ nspam.com>

在comp.lang.c中写道:


我正和某人谈论fgets和他说fgets把

\ n放在字符串中而不是\0。我决定测试这个假设,因为我的

文档没有说fgets是否在字符串文字之后放置了\0。



这个人是谁,他有什么凭据可以让你

认为他是对的?

更重要的是,字符串文字与什么有什么关系?

fgets()与字符串文字没有任何关系。它从流中读取输入

。字符串文字在你的源代码中引用了字符串

代码,而不是从流中读取的输入。


Strlen是

我以前决定没有添加\0。如何为

字符串添加它?我的代码。



我肯定不明白你上面要做的是什么。如果你的
有一个字符数组,在数组的范围内不包含至少一个''\ 0''
,那么它不是一个字符串。在一个不是字符串的字符数组上调用
strlen(),由于没有''\0'',因此会产生未定义的行为。 strlen()用于字符串,

不是任意乱码字符数组。


int main(void){

char输入[10];

fgets(输入,sizeof(输入),stdin);



在此调用中,fgets()最多可接受来自stdin的9个字符。如果在第九个字符之前收到''\ n'',则更少




它会将这些字符存储到数组的连续元素中。<无论是否在数组中存储9个或更少的字符,它都会在它存储的最后一个字符之后将''\0''放入数组中。<\\ n>
br />

具体说明标准关于fgets()的内容:


" fgets函数最多读取一个小于<的数量< br>
由流指向的流指定的字符到由s指向的数组的
。在

新行字符(保留)或文件结束后,不会读取其他字符。一个

null字符在最后一个字符读取后立即写入

到数组中。


所以你的某人 ;他们认为是错误的。


printf("%i \ n",strlen(input));



strlen()函数返回一个size_t类型的值,因为

printf()是一个可变函数,函数调用确实没有兑换

任何转换。 "%I"是signed int的转换说明符,

size_t永远不能是signed int,因为它总是一个无符号类型。


如果你需要打印一个相当小的size_t值,你可以这样做:

这个:


printf("%u",(unsigned)strlen(input));


}


比printf报告的字符多一个字符。



我无法解析你上面写的句子。


这样会好得多:


1.告诉我们你输入程序的原因


2.告诉我们程序的输出是什么。


无论你认为你用你的程序证明了什么,

fgets()函数肯定会产生''\0''终止正确

字符串,但在两种特殊情况下除外:


"如果成功,fgets函数返回s。如果遇到文件结尾为
并且没有字符读入数组,则数组的内容保持不变,并返回空指针。 />
如果在操作期间发生读取错误,则数组内容为

indeterminate并返回空指针。


要么这些例外情况很容易被事实识别,即fgets()返回NULL,而不是返回传递给它的指针


< br $> b $ b -

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://c-faq.com/

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.club.cc.cmu.edu/~ajo /docs/FAQ-acllc.html




" Ben Bacarisse" < be ******** @ bsb.me.ukwrote in message

news:87 ************ @ bsb.me.uk .. 。


你在哪里见到人们聊聊C?



在名为c-prog的yahoogroup上。它也适用于C ++。


>我决定测试这个假设,因为我的
文档没有''如果fgets在字符串文字后放置\0。 Strlen
是我以前决定没有添加的\0。



是什么让你这么想的?必须有一个空字节或strlen不会

工作。


>如何为
字符串?



它已经存在。如果fgets耗尽了空间,它将停止一个简短的
,这样它总是可以将终止空值放在缓冲区中。你

可能得不到''\ n''但你总会得到0.


>我的代码。

int main(void){
char input [10];
fgets(input,sizeof(input),stdin);
printf("%i \\ \\ n,strlen(输入));



更好的是:

printf("%i \ n",(int)strlen(输入));



哦,看起来像演员。你问printf要做什么?


因为%i需要一个int而不是size_t。或者,使用%zu。作为

格式,因为你似乎正在使用接近C99的东西。


>}

一个比printf报道的字符更多的字符。



''\ n''是一个字符。如果你输入abc并点击返回,fgets将五个东西放入输入数组:''a'','b'',''c'',''\ n''和''\'''。这个

是一个四字符串和strlen报告4.


-

Ben。



I was talking with someone about fgets and he said that fgets puts the
\n in a string but not \0. I decided to test this assumption because my
documentation didn''t say if fgets put \0 after a string literal. Strlen was
what I used to decide the \0 was not added. How can it be added for a
string? My code.

int main (void) {
char input[10];
fgets (input, sizeof(input), stdin);
printf ("%i\n", strlen(input));
}

One more character than what was typed printf reported.

Bill

解决方案

"Bill Cunningham" <no****@nspam.comwrites:

I was talking with someone about fgets and he said that fgets puts the
\n in a string but not \0.

Where do you meet people to chat about C?

I decided to test this assumption because my
documentation didn''t say if fgets put \0 after a string literal. Strlen was
what I used to decide the \0 was not added.

What made you think that? There must be a null byte or strlen won''t
work.

How can it be added for a
string?

It is there already. If fgets runs out of room it will stop one short
just so it can always put the terminating null in the buffer. You
may not get a ''\n'' but you will always get a 0.

My code.

int main (void) {
char input[10];
fgets (input, sizeof(input), stdin);
printf ("%i\n", strlen(input));

Better is:
printf ("%i\n", (int)strlen(input));

since %i needs an int not a size_t. Alternatively use "%zu" as the
format since you seem to be using somthing close to C99.

}

One more character than what was typed printf reported.

The ''\n'' is a character. If you type abc and hit return, fgets put
five things into the input array: ''a'', ''b'', ''c'', ''\n'' and ''\0''. This
is a four character string and strlen reports 4.

--
Ben.


On Mon, 09 Jun 2008 01:54:12 GMT, "Bill Cunningham" <no****@nspam.com>
wrote in comp.lang.c:

I was talking with someone about fgets and he said that fgets puts the
\n in a string but not \0. I decided to test this assumption because my
documentation didn''t say if fgets put \0 after a string literal.

Who was this person, and what credentials did he have to make you
think he was right?

More importantly, what does a string literal have to do with anything?
fgets() has nothing at all to do with string literals. It reads input
from a stream. String literals are quoted strings in your source
code, not input read from a stream.

Strlen was
what I used to decide the \0 was not added. How can it be added for a
string? My code.

I surely don''t understand what you were trying to do above. If you
had an array of characters that does not contain at least one ''\0''
within the bounds of the array, then it is not a string. Calling
strlen() on an array of characters that is not a string, due to the
lack of a ''\0'', produces undefined behavior. strlen() is for strings,
not arbitrary arrays of garbage characters.

int main (void) {
char input[10];
fgets (input, sizeof(input), stdin);

In this call, fgets() will accept up to 9 characters from stdin. Fewer
if it receives a ''\n'' before the ninth character.

It will store these characters into consecutive elements of the array.
Regardless of whether it stores 9 or fewer characters in the array, it
will place a ''\0'' into the array after the last character it stores.

Here is specifically what the standard says about fgets():

"The fgets function reads at most one less than the number of
characters specified by n from the stream pointed to by stream into
the array pointed to by s. No additional characters are read after a
new-line character (which is retained) or after end-of-file. A
null character is written immediately after the last character read
into the array."

So your "someone" is wrong in their opinion.

printf ("%i\n", strlen(input));

The strlen() function returns a value of type size_t, and since
printf() is a variadic function, the function call does not perform
any conversion. "%i" is a conversion specifier for signed int, and
size_t can never be a signed int since it is always an unsigned type.

If you need to print a reasonably small value of size_t, you could do
this:

printf("%u", (unsigned)strlen(input));

}

One more character than what was typed printf reported.

I can''t parse the sentence you have written above.

It would be much better to:

1. Show us why you typed into the program

2. Show us what the output of the program was.

Regardless of what you think you proved to yourself with your program,
the fgets() function most surely does produce a ''\0'' terminated proper
string, except under two exceptional cases:

"The fgets function returns s if successful. If end-of-file is
encountered and no characters have been read into the array, the
contents of the array remain unchanged and a null pointer is returned.
If a read error occurs during the operation, the array contents are
indeterminate and a null pointer is returned."

Either of these exceptional cases is easily recognized by the fact
that fgets() returns NULL, instead of returning the pointer that was
passed in to it.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html



"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...

Where do you meet people to chat about C?


On a yahoogroup called c-prog. It''s for C++ too.

>I decided to test this assumption because my
documentation didn''t say if fgets put \0 after a string literal. Strlen
was
what I used to decide the \0 was not added.


What made you think that? There must be a null byte or strlen won''t
work.

>How can it be added for a
string?


It is there already. If fgets runs out of room it will stop one short
just so it can always put the terminating null in the buffer. You
may not get a ''\n'' but you will always get a 0.

>My code.

int main (void) {
char input[10];
fgets (input, sizeof(input), stdin);
printf ("%i\n", strlen(input));


Better is:
printf ("%i\n", (int)strlen(input));

Oh that looks like a cast. What are you asking printf to do?

since %i needs an int not a size_t. Alternatively use "%zu" as the
format since you seem to be using somthing close to C99.

>}

One more character than what was typed printf reported.


The ''\n'' is a character. If you type abc and hit return, fgets put
five things into the input array: ''a'', ''b'', ''c'', ''\n'' and ''\0''. This
is a four character string and strlen reports 4.

--
Ben.



这篇关于fgets问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆