std::fgetc, std::getc - cppreference.com (original) (raw)

C++

Compiler support
Freestanding and hosted
Language
Standard library
Standard library headers
Named requirements
Feature test macros (C++20)
Language support library
Concepts library (C++20)
Diagnostics library
Memory management library
Metaprogramming library (C++11)
General utilities library
Containers library
Iterators library
Ranges library (C++20)
Algorithms library
Strings library
Text processing library
Numerics library
Date and time library
Input/output library
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Technical specifications
Symbols index
External libraries

[edit]

Input/output library

I/O manipulators
Print functions (C++23)
C-style I/O
Buffers
basic_streambuf
basic_filebuf
basic_stringbuf
basic_spanbuf(C++23)
strstreambuf(C++98/26*)
basic_syncbuf(C++20)
Streams
Abstractions
ios_base
basic_ios
basic_istream
basic_ostream
basic_iostream
File I/O
basic_ifstream
basic_ofstream
basic_fstream
String I/O
basic_istringstream
basic_ostringstream
basic_stringstream
Array I/O
basic_ispanstream(C++23)
basic_ospanstream(C++23)
basic_spanstream(C++23)
istrstream(C++98/26*)
ostrstream(C++98/26*)
strstream(C++98/26*)
Synchronized Output
basic_osyncstream(C++20)
Types
streamoff
streamsize
fpos
Error category interface
iostream_category(C++11)
io_errc(C++11)

[edit]

C-style I/O

Types and objects
FILE fpos_t stdinstdoutstderr
Functions
File access fopen freopen fclose fflush fwide setbuf setvbuf Direct input/output freadfwrite Unformatted input/output fgetcgetc fgets fputcputc fputs getchar gets(until C++14) putchar puts ungetc fgetwcgetwc fgetws fputwcputwc fputws getwchar putwchar ungetwc Formatted input scanffscanfsscanf vscanfvfscanfvsscanf(C++11)(C++11)(C++11) wscanffwscanfswscanf vwscanfvfwscanfvswscanf(C++11)(C++11)(C++11) Formatted output printffprintfsprintfsnprintf(C++11) vprintfvfprintfvsprintfvsnprintf(C++11) wprintffwprintfswprintf vwprintfvfwprintfvswprintf File positioning ftell fgetpos fseek fsetpos rewind Error handling clearerr feof ferror perror Operations on files remove rename tmpfile tmpnam

[edit]

| Defined in header | | | | ------------------------------------------------------------------------------------------ | | | | int fgetc( std::FILE* stream ); int getc( std::FILE* stream ); | | |

Reads the next character from the given input stream.

Contents

[edit] Parameters

stream - to read the character from

[edit] Return value

The obtained character on success or EOF on failure.

If the failure has been caused by end of file condition, additionally sets the eof indicator (see std::feof()) on stream. If the failure has been caused by some other error, sets the error indicator (see std::ferror()) on stream.

[edit] Example

Run this code

#include #include   int main() { int is_ok = EXIT_FAILURE; FILE* fp = std::fopen("/tmp/test.txt", "w+"); if (!fp) { std::perror("File opening failed"); return is_ok; }   int c; // Note: int, not char, required to handle EOF while ((c = std::fgetc(fp)) != EOF) // Standard C I/O file reading loop std::putchar(c);   if (std::ferror(fp)) std::puts("I/O error when reading"); else if (std::feof(fp)) { std::puts("End of file reached successfully"); is_ok = EXIT_SUCCESS; }   std::fclose(fp); return is_ok; }

Output:

End of file reached successfully

[edit] See also

gets(deprecated in C++11)(removed in C++14) reads a character string from stdin (function) [edit]
fputcputc writes a character to a file stream (function) [edit]
ungetc puts a character back into a file stream (function) [edit]
C documentation for fgetc, getc