`

fgets, fgetws

阅读更多

Run-Time Library Reference
fgets, fgetws

Get a string from a stream.

char *fgets( 
   char *string,
   int n,
   FILE *stream 
);
wchar_t *fgetws( 
   wchar_t *string,
   int n,
   FILE *stream 
);

Parameters

string
Storage location for data.
n
Maximum number of characters to read.
stream
Pointer to FILE structure.

Return Value

Each of these functions returns stringNULL is returned to indicate an error or an end-of-file condition. Use feof orferror to determine whether an error occurred.

Remarks

The fgets function reads a string from the input stream argument and stores it in stringfgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n – 1, whichever comes first. The result stored in string is appended with a null character. The newline character, if read, is included in the string.

fgetws is a wide-character version of fgets.

fgetws reads the wide-character argument string as a multibyte-character string or a wide-character string according to whether stream is opened in text mode or binary mode, respectively. For more information about using text and binary modes in Unicode and multibyte stream-I/O, see Text and Binary Mode File I/O and Unicode Stream I/O in Text and Binary Modes.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_fgetts fgets fgets fgetws

Requirements

Function Required header Compatibility
fgets <stdio.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
fgetws <stdio.h> or <wchar.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example

// crt_fgets.c
/* This program uses fgets to display
 * a line from a file on the screen.
 */

#include <stdio.h>

int main( void )
{
   FILE *stream;
   char line[100];

   if( (stream = fopen( "crt_fgets.txt", "r" )) != NULL )
   {
      if( fgets( line, 100, stream ) == NULL)
         printf( "fgets error\n" );
      else
         printf( "%s", line);
      fclose( stream );
   }
}

Input: crt_fgets.txt

Line one.
Line two.

Output

Line one.

See Also

Stream I/O Routines | fputs | gets | puts | Run-Time Routines and .NET Framework Equivalents

分享到:
评论

相关推荐

    如何解决fgets读取popen内容阻塞的问题

    如何解决fgets读取popen内容阻塞的问题

    fgets与fputs函数

    fgets函数详解 fgets函数 从流中读一行或指定个字符,  原型是char *fgets(char *s, int n, FILE *stream);  从流中读取n-1个字符,除非读完一行,参数s是来接收字符串,如果成功则返回s的指针,否则返回NULL。...

    fgets_fgets_

    Implementation of fgets file handling function in c.

    c语言fgets fputs 读写文件

    读字符串函数fgets函数的功能是从指定的文件中读一个字符串到字符数组中,函数调用的形式为: fgets(字符数组名,n,文件指针)

    C语言文件操作中 fgets与fputs 函数详解

    C语言文件操作中 fgets、fputs 函数详解 先给出api fgets 语法: #include char *fgets( char *str, int num, FILE *stream ); 函数fgets()从给出的文件流中读取[num – 1]个字符并且把它们转储到str(字符串)中....

    标准I/O库函数:fgets与gets比较分析

    函数名:fgets  功能:从流中读取一个字符串  用法:char *fgets(char *string,int n,FILE *stream);  形参注释:  *string:结果数据的首地址;n-1:读入数据块的长度,其默认值为1k,即1024;stream文件...

    fgets.c

    fgets.c

    fgets函数与fputs函数

    文章简单介绍了fgets函数和fputs函数的应用及不同

    fgets函数用法

    详细描述了fgets函数的用法,该函数可读取文件中的字符串,可供C/CPP开发人员参考。

    C语言中fgets和fscanf区别详解

    C语言中fgets和fscanf区别详解 一、作用上的大概区别: ①fgets:从文件中读取一行数据存入缓冲区(fgets遇到回车才会结束,不对空格和回车做任何转换就录入到缓冲区,结束后再往缓冲区写多一个\0,所以它是读一行...

    在C语言中,文件读写操作通常使用标准库中的fopen(), fclose(), fgets(), fputs(), fscanf

    在C语言中,文件读写操作通常使用标准库中的fopen(), fclose(), fgets(), fputs(), fscanf(), fprintf()等函数。以下是一个简单的示例,展示了如何使用这些函数来读取和写入文件。 写入文件示例 c #include ...

    C语言中输入函数(scanf()、fgets()和gets())的区别详解

    主要给大家介绍了关于C语言中三种输入函数(scanf()、fgets()和gets())区别的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。

    fgets( )和fputs( )(建议用其完全代替gets和puts函数)

    很早之前,本人开始接触C语言的时候整理的笔记,包含了函数fgets( )和fputs( )的总结和使用心得,以及与对应的无f前缀函数的区别分析,现在分享给大家,仅代表个人观点,由于能力有限,难免有纰漏,仅供学习交流.

    EDA/PLD中的标准I/O库函数:fgets与gets比较分析

    函数名:fgets  功能:从流中读取一个字符串  用法:char *fgets(char *string,int n,FILE *stream);  形参注释:  *string:结果数据的首地址;n-1:一次读入数据块的长度,其默认值为1k,即1024;stream...

    c语言文件读写函数

    字符读写函数:(fgetc和fputc) ...fgets函数:一般格式为:fgets(str,n,fp) 读写数据块函数(fread和fwrite) 一般调用形式为:fread(buffer,size,count,fp) fwrite(buffer,size,count,fp)

Global site tag (gtag.js) - Google Analytics