Table of Contents

fgetc

Description

Read a single character from a file at the current stream position.

Function prototype

int fgetc <voidptr:hFile>

Arguments

Name Type Comment
hFile voidptr A handle to a file created by fopen or fopen_s.

Return value

The value of the character in the file at the current stream position, or EOF (-1) if an error occurred.

Example

// Ask user for filename
string FileName
set FileName <file.OpenDlg "txt" NULL "Text files (*.txt)|*.txt|All files (*.*)|*.*|">
if <iseq 0 <strlen FileName>>
  return 0
endif

// Open file
voidptr fp
set fp <fopen FileName "r">
assert fp "Cannot open file!"

// Prepare some variables
char c
set c 0
string s

// Read file character-by-character using fgetc, and add to string

do
  set c <fgetc fp>
  if <isgt c 0>
    set s <strcat s c>
  endif
while <isgt c 0>

echo s // Write file contents (in string) to event log

fclose fp // Close file

Comments

None.

See also