曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 5085|回复: 4
打印 上一主题 下一主题

fgets,gets

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
跳转到指定楼层
楼主
发表于 2008-11-16 23:09:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
NAME


[blockquote]fgets - get a string from a stream[/blockquote]

SYNOPSIS


[blockquote]
#include <stdio.h>



char *fgets(char *restrict s, int n, FILE *restrict stream);



[/blockquote]

DESCRIPTION
[blockquote]The fgets() function shall read bytes from stream into the array pointed to by s, until n-1 bytes
are read, or a  is read and transferred to s, or an end-of-file condition is encountered. The string is then
terminated with a null byte.


[sup][CX][/sup] The
fgets() function may mark the st_atime field of the file associated with stream for update. The
st_atime field shall be marked for update by the first successful execution of fgetc(), fgets(), fgetwc(), fgetws(), fread(), fscanf(), getc(), getchar(), gets(), or scanf() using stream that returns data not supplied by a prior call to ungetc() or ungetwc().

[/blockquote]

RETURN VALUE


[blockquote]
Upon successful completion, fgets() shall return s. If the stream is at end-of-file, the end-of-file indicator for
the stream shall be set and fgets() shall return a null pointer. If a read error occurs, the error indicator for the stream
shall be set, fgets() shall return a null pointer, [sup][CX][/sup]  and shall set errno to indicate the error.

[/blockquote]

ERRORS


[blockquote]
Refer to fgetc().

[/blockquote]



The following sections are informative.

EXAMPLES


[blockquote]
Reading Input


The following example uses fgets() to read each line of input. {LINE_MAX}, which defines the maximum size of the input
line, is defined in the [/url] header.


[pre]#include
...
char line[LINE_MAX];
...
while (fgets(line, LINE_MAX, fp) != NULL) {
...
}
...

[/pre]
[/blockquote]

APPLICATION USAGE


[blockquote]
None.

[/blockquote]

RATIONALE


[blockquote]
None.

[/blockquote]

FUTURE DIRECTIONS


[blockquote]
None.

[/blockquote]

SEE ALSO


[blockquote]
[url=http://www.opengroup.org/onlinepubs/009695399/functions/fopen.html]fopen()
, fread(), gets(), the
Base Definitions volume of IEEE Std 1003.1-2001, [url=http://www.opengroup.org/onlinepubs/009695399/basedefs/stdio.h.html][/url]

[/blockquote]

CHANGE HISTORY


[blockquote]
First released in Issue 1. Derived from Issue 1 of the SVID.

[/blockquote]

Issue 6



Extensions beyond the ISO C standard are marked.


The prototype for fgets() is changed for alignment with the ISO/IEC 9899:1999 standard.

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
沙发
 楼主| 发表于 2009-2-11 01:24:39 | 只看该作者

fgets 举例

测试代码(测试相关文件--和程序在同一个文件夹下的名字为new-1的文件)
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>

int main()
{
FILE *new;
char buf[100];
int n;
char *ptr = NULL;

new = fopen("new-1", "r");

if( fgets(buf, 6, new) == NULL ){
        printf("the file is blank!\\n");
        exit(1);
}

ptr = fgets(buf, 6, new);

printf("%s\\n",buf);

printf("%c\\n",*ptr);

printf("%c\\n",*(ptr+1) );

if( fgets(buf, 6, new) == NULL ){
        printf("EOF is encountered!\\n");
        exit(1);
}

return 0;
}

输出结果
[root@localhost Desktop]# ./fopen_read.exe
ine!

i
n
EOF is encountered!

分析
1、如果 new-1 文件为空,则打开文件流也马上到达末尾,故返回一个空指针,所以打印出“the file is blank!”。

2、现在假定文件new-1中的内容为: one line!
那么在
if( fgets(buf, 6, new) == NULL ){
        printf("the file is blank!\\n");
        exit(1);
}
后,文件流指针已经移向字母 l 处(因为指定读取6个字符实际上只读取6-1=5个字符)。所以,在执行到 ptr = fgets(buf, 6, new);
这里时,实际上就是从 i 开始读,故看到输出结果。

当最后一次测试读的时候,文件流指针已经到达文件末尾,如上帖所说的,也要返回一个空指针,所以输出:
EOF is encountered!

3、如果需要重新定位,那么可以先在第一次测试后关闭掉文件流然后再打开一次即可。

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
板凳
 楼主| 发表于 2008-11-16 23:10:09 | 只看该作者
fgets(由文件中读取一字符串)
  相关函数
  open,fread,fscanf,getc
  表头文件
  include<stdio.h>
  定义函数
  char * fgets(char * s,int size,FILE * stream);
  函数说明
  fgets()用来从参数stream所指的文件内读入字符并存到参数s所指的内存空间,直到出现换行字符、读到文件尾或是已读了size-1个字符为止,最后会加上NULL作为字符串结束。
  返回值
  gets()若成功则返回s指针,返回NULL则表示有错误发生。
  范例
  #include<stdio.h>
  main()
  {
  char s[80];
  fputs(fgets(s,80,stdin),stdout);
  }
  执行
  this is a test /*输入*/
  this is a test /*输出*/

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
地板
 楼主| 发表于 2009-2-10 21:38:19 | 只看该作者
|---------------------------------|
|  beginning linux programming 4th   |
|---------------------------------|

fgets 函数从一个输入文件流(input file stream)中读取一个字符串。
#include <stdio.h>
char *fgets(char *s, int n, FILE *stream);
char *gets(char *s);

fgets 把字符写到由 s 指向的字符串空间,直到遇到一个新行,或者 n-1 个字符已被传输,或者已经达到文件末尾,这三者中不论那种情况第一个发生,都会结束一次写的操作。新行传送到接收字符串空间后,在末尾添加空字节(\\0).在任何一次调用中,最大只有 n-1 个字节被传递,这是因为一个空字节必须添加在最后以标志字符串的结束以及凑成总数达到 n 个字节。

当成功完成时,fgets 返回一个指向字符串s的指针。如果流在文件的末尾,则设置 EOF 指示符,并且 fgets 返回一个空指针。如果一个读错误发生,fgets 也返回空指针以及设置 errno 以指明错误的类型。

gets 和 fgets 类似,除了它是从标准输入读并且忽略任何遇到的新行。它也会在接收字符串的末尾添加一个空字节。

注意:gets 不会限制传送的字符个数;所以这会过度使用传输缓冲区。因此,你应该避免使用它,而去使用 fgets 来代替。许多安全事故在程序中跟踪检查函数时,往往都跟某种或其它的缓冲区溢出有关。这是其中的一种,所以要相当小心!

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
5#
 楼主| 发表于 2009-4-27 16:26:10 | 只看该作者

fgets 读取标准输入

从标准输入读入一行数据时,若没有超出函数参数 size 的限制,那么将会读到一个换行符 '\\n' 。

测试代码
#include <stdio.h>
#include <string.h>

void strip_return(char *string_to_strip);

int main()
{
    char tmp_str[10];
    printf("Please input a string: \\n");
    fgets(tmp_str, 10, stdin);
    strip_return(tmp_str);
    return 0;
}

void strip_return(char *string_to_strip)
{
    int len;
   
    len = strlen(string_to_strip);
    printf("The input string len: %d\\n", len);
}
执行与输出
beyes@linux-beyes:~/C> ./string_to_strip.exe
Please input a string:
hello
The input string len: 6
说明
可见,包括了换行符 '\\n'

有时需要把 '\\n' 吃掉:
if (string_to_strip[len - 1] == '\\n') string_to_strip[len - 1] = '\\0';
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-5-3 23:30 , Processed in 0.072336 second(s), 21 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表