首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::fgets

Defined in header <cstdio>

?

?

char* fgets( char* str, int count, std::FILE* stream );

?

?

最多读count - 1指定文件流中的字符,并将它们存储在str如果文件结束或找到换行符,则解析停止。str将包含这个换行符。如果没有发生错误,则在上次写入的字符之后立即在位置写入空字符。str...

参数

str

-

pointer to an element of a char array

count

-

maximum number of characters to write (typically the length of str)

stream

-

file stream to read the data from

返回值

str成功时,失败时为空指针。

如果故障是由文件结束条件造成的,则另外设置EOF指标%28见std::feof()29%stream所指向的数组的内容。str在这种情况下是不会改变的。

如果故障是由其他错误引起的,则设置误差指标%28见std::ferror()29%stream所指向的数组的内容。str是不确定的%28,它甚至可能不是以空结尾的%29。

二次

代码语言:javascript
复制
#include <iostream>
#include <cstdio>
#include <cstdlib>
 
int main()
{
    std::FILE* tmpf = std::tmpfile();
    std::fputs("Alan Turing\n", tmpf);
    std::fputs("John von Neumann\n", tmpf);
    std::fputs("Alonzo Church\n", tmpf);
 
    std::rewind(tmpf);
    char buf[8];
    while (std::fgets(buf, sizeof buf, tmpf) != NULL) {
        std::cout << '"' << buf << '"' << '\n';
    }
}

二次

产出:

二次

代码语言:javascript
复制
"Alan Tu"
"ring
"
"John vo"
"n Neuma"
"nn
"
"Alonzo "
"Church
"

二次

另见

scanffscanfsscanf

reads formatted input from stdin, a file stream or a buffer (function)

gets (until C++14)

reads a character string from stdin (function)

fputs

writes a character string to a file stream (function)

c fget文档

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com