关于fgets的问题 [英] question on fgets

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

问题描述

假设fgets用于读取一行输入。

char str [1024];

fgets(str,sizeof(str),stdin);

读完同一行的某些字符后,如果遇到文件结尾为
,fgets会返回''str''参数并设置EOF

流的指标?或者它会返回字符串参数并且

仅在后续调用中设置EOF指示符?


请澄清


谢谢

V.Subramanian

Suppose fgets is used to read a line of input.
char str[1024];
fgets(str, sizeof(str), stdin);
After reading some characters on the same line, if end-of-file is
encountered, will fgets return the ''str'' parameter and set EOF
indicator for the stream ? Or will it return the string argument and
set EOF indicator only on subsequent call ?

Kindly clarify

Thanks
V.Subramanian

推荐答案

su ************** @ yahoo.com ,印度写道:
su**************@yahoo.com, India wrote:

假设fgets用于读取一行输入。

char str [1024];

fgets(str,sizeof(str),stdin);
Suppose fgets is used to read a line of input.
char str[1024];
fgets(str, sizeof(str), stdin);



除了类型之外,不需要sizeof的括号。

The parenthesis to sizeof is not required except for types.


读取相同的字符后如果遇到文件结尾是
,那么fgets会返回''str''参数并为流设置EOF

指标吗?或者它会返回字符串参数和

仅在后续调用中设置EOF指标?


请澄清
After reading some characters on the same line, if end-of-file is
encountered, will fgets return the ''str'' parameter and set EOF
indicator for the stream ? Or will it return the string argument and
set EOF indicator only on subsequent call ?

Kindly clarify



如果在读完一个或多个字符后检测到文件结尾,则
然后fgets将返回''str''。否则它将返回一个空指针。

If end-of-file is detected after one or more characters have been read,
then fgets will return ''str''. Otherwise it will return a null pointer.


su ************** @ yahoo.com ,印度< su ************** @ yahoo。编写:
su**************@yahoo.com, India <su**************@yahoo.comwrote:

假设fgets用于读取一行输入。

char str [1024];

fgets(str,sizeof(str),stdin);

读完同一行上的一些字符后,如果文件结尾遇到
,fgets会返回''str''参数并为流设置EOF

指标?或者它会返回字符串参数和

仅在后续调用中设置EOF指标?
Suppose fgets is used to read a line of input.
char str[1024];
fgets(str, sizeof(str), stdin);
After reading some characters on the same line, if end-of-file is
encountered, will fgets return the ''str'' parameter and set EOF
indicator for the stream ? Or will it return the string argument and
set EOF indicator only on subsequent call ?



这是C89标准所说的:


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

indeterminate并返回空指针。


所以如果该文件的最后一行最后没有''\ n'','str''
返回
(命中文件结尾不是错误)和NULL

仅在随后的fgets()调用中。


问候,Jens

-

\ Jens Thoms Toerring ___ jt@toerring.de

\ __________________________ http://toerring.de


" su ******* *******@yahoo.com,印度写道:
"su**************@yahoo.com, India" wrote:

>

假设fgets用于读取一行输入。

char str [ 1024];

fgets(str,sizeof(str),stdin);

读取同一行的某些字符后,如果文件结尾是
遇到
,fgets会返回''str''参数并为流设置EOF

指标吗?或者它会返回字符串参数

并仅在后续调用中设置EOF指标?
>
Suppose fgets is used to read a line of input.
char str[1024];
fgets(str, sizeof(str), stdin);
After reading some characters on the same line, if end-of-file is
encountered, will fgets return the ''str'' parameter and set EOF
indicator for the stream ? Or will it return the string argument
and set EOF indicator only on subsequent call ?



为什么问这里?所有你需要做的就是阅读C标准,而你

会找到以下内容:


7.19.7.2 fgets功能


概要

[#1]

#include< stdio.h>

char * fgets(char * restrict s,int n,

FILE *限制流);


描述


[#2] fgets函数读取最多比n指定的字符数n / b
由流指向的流指向由s指向的数组。新行字符(保留
后)或文件结束后不会读取额外的

字符。在读入数组的最后一个字符后立即写入空字符




返回


[# 3]如果成功,fgets函数返回s。如果遇到了结束

文件并且没有读入任何字符

数组,则数组的内容保持不变并且
$ b $返回b null指针。如果在

操作期间发生读取错误,则数组内容不确定并返回null

指针。


- -

[邮件]:Chuck F(cinefalconer at maineline dot net)

[page]:< http://cbfalconer.home.att.net>

尝试下载部分。

Why ask here? All you have to do is read the C standard, and you
will find the following:

7.19.7.2 The fgets function

Synopsis
[#1]
#include <stdio.h>
char *fgets(char * restrict s, int n,
FILE * restrict stream);

Description

[#2] 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.

Returns

[#3] 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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


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

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