GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::fgets(3) C++ Standard Libary std::fgets(3)

std::fgets - std::fgets


Defined in header <cstdio>
char* fgets( char* str, int count, std::FILE* stream );


Reads at most count - 1 characters from the given file stream and stores them in the
character array pointed to by str. Parsing stops if a newline character is found, in
which case str will contain that newline character, or if end-of-file occurs. If
bytes are read and no errors occur, writes a null character at the position
immediately after the last character written to 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 on success, null pointer on failure.


If the end-of-file condition is encountered, sets the eof indicator on stream (see
std::feof()). This is only a failure if it causes no bytes to be read, in which case
a null pointer is returned and the contents of the array pointed to by str are not
altered (i.e. the first byte is not overwritten with a null character).


If the failure has been caused by some other error, sets the error indicator (see
std::ferror()) on stream. The contents of the array pointed to by str are
indeterminate (it may not even be null-terminated).


POSIX additionally requires that fgets sets errno if it encounters a failure other
than the end-of-file condition.


Although the standard specification is unclear in the cases where count<=1, common
implementations do


* if count < 1, do nothing, report error
* if count == 1,


* some implementations do nothing, report error,
* others read nothing, store zero in str[0], report success

// Run this code


#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) != nullptr) {
std::cout << '"' << buf << '"' << '\n';
}
}


"Alan Tu"
"ring
"
"John vo"
"n Neuma"
"nn
"
"Alonzo "
"Church
"


scanf reads formatted input from stdin, a file stream or a buffer
fscanf (function)
sscanf
gets reads a character string from stdin
(deprecated in C++11) (function)
(removed in C++14)
fputs writes a character string to a file stream
(function)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.