Skip to content

Latest commit

 

History

History
125 lines (91 loc) · 3.68 KB

getc-getwc.md

File metadata and controls

125 lines (91 loc) · 3.68 KB
description title ms.date api_name api_location api_type topic_type f1_keywords helpviewer_keywords ms.assetid
Learn more about: getc, getwc
getc, getwc
4/2/2020
getwc
getc
_o_getc
_o_getwc
msvcrt.dll
msvcr80.dll
msvcr90.dll
msvcr100.dll
msvcr100_clr0400.dll
msvcr110.dll
msvcr110_clr0400.dll
msvcr120.dll
msvcr120_clr0400.dll
ucrtbase.dll
api-ms-win-crt-stdio-l1-1-0.dll
DLLExport
apiref
_gettc
getwc
_gettchar
getc
characters, reading
_gettc function
getwchar function
streams, reading characters from
reading characters from streams
getc function
getwc function
gettc function
354ef514-d0c7-404b-92f5-995f6a834bb3

getc, getwc

Read a character from a stream.

Syntax

int getc(
   FILE *stream
);
wint_t getwc(
   FILE *stream
);

Parameters

stream
Input stream.

Return value

Returns the character read. To indicate a read error or end-of-file condition, getc returns EOF, and getwc returns WEOF. For getc, use ferror or feof to check for an error or for end of file. If stream is NULL, getc and getwc invoke the invalid parameter handler, as described in Parameter validation. If execution is allowed to continue, these functions return EOF (or WEOF for getwc), and set errno to EINVAL.

For more information about return codes, see errno, _doserrno, _sys_errlist, and _sys_nerr.

Remarks

Each routine reads a single character from a file at the current position and increments the associated file pointer (if defined) to point to the next character. The file is associated with stream.

These functions lock the calling thread and are therefore thread-safe. For a non-locking version, see _getc_nolock, _getwc_nolock.

Routine-specific remarks follow.

Routine Remarks
getc Same as fgetc, but implemented as a function and as a macro.
getwc Wide-character version of getc. Reads a multibyte character or a wide character according to whether stream is opened in text mode or binary mode.

By default, this function's global state is scoped to the application. To change this behavior, see Global state in the CRT.

Generic-text routine mappings

TCHAR.H routine _UNICODE and _MBCS not defined _MBCS defined _UNICODE defined
_gettc getc getc getwc

Requirements

Routine Required header
getc <stdio.h>
getwc <stdio.h> or <wchar.h>

For more compatibility information, see Compatibility.

Example

// crt_getc.c
// Use getc to read a line from a file.

#include <stdio.h>

int main()
{
    char buffer[81];
    int i, ch;
    FILE* fp;

    // Read a single line from the file "crt_getc.txt".

    fopen_s(&fp, "crt_getc.txt", "r");
    if (!fp)
    {
       printf("Failed to open file crt_getc.txt.\n");
       exit(1);
    }

    for (i = 0; (i < 80) && ((ch = getc(fp)) != EOF)
                         && (ch != '\n'); i++)
    {
        buffer[i] = (char) ch;
    }

    // Terminate string with a null character
    buffer[i] = '\0';
    printf( "Input was: %s\n", buffer);

    fclose(fp);
}

Input: crt_getc.txt

Line one.
Line two.

Output

Input was: Line one.

See also

Stream I/O
fgetc, fgetwc
_getch, _getwch
putc, putwc
ungetc, ungetwc