| |||
| Home > RealView Debugger Predefined Macros > Alphabetical predefined macro reference > fgetc | |||
Reads a byte from a file.
int fgetc (fileid) intfileid;
where:
fileidThe ID of the file containing the next byte to be
read. This must be a user-defined .
See Window and file numbers for details.fileid
This macro returns the contents of the next byte from a file.
The fgetc macro name is short for [file
getc() ], where file indicates that
the macro operates on a file, and getc is the
standard function for getting a character from a user defined file.
This is distinct from the getchar function,
which can only retrieve a character from the standard input, and
is typically the keyboard.
fgetc returns the contents of the next
memory location byte from the specified file. You define the identity
of the file with the fopen macro (see fopen on fopen), or the FOPEN command
(see FOPEN on FOPEN). Any file used to read, or get, the
contents of the next byte, must be opened in read mode.
intReturns the contents of the next byte of memory
from a user specified file or window. Returns the value -1 if either
an end-of-file mark (EOF) or an error is encountered.
The file read from must be opened in read mode, for example:
fopen(100,”c:\\myfiles\\data_in.txt”,”r”)
This example shows how to use fgetc, together
with fopen and fputc:
define /R void copyFile()
{
int retval;
int ch;
// Create data file to read
retval = fopen(100,”c:\\myfiles\\data_in.txt”,”w”);
if (retval < 0)
error(2,"Cannot open file for writing!\n",101);
else {
retval = fwrite("1234567890\n1234567890\n1234567890", 1, 32, 100);
$vclose 100$;
fopen(100,”c:\\myfiles\\data_in.txt”,”r”); // open for read-only
if (retval < 0)
error(2,"Source file not opened!\n",101);
else
fopen(200,”c:\\myfiles\data_out.txt”,”w”); // open for writing
if (retval < 0)
error(2,"Destination file not opened!\n",101);
else
do {
ch = fgetc(100); // fgetc()
if (ch < 0)
$printf "Finished copying the file!"$;
else
fputc(ch,200); // fputc()
} while (ch > 0);
}
$vclose 100$;
$vclose 200$;
}
}
.