How do I write the return value from getchar() function to a file? I tried the following and it wrote integers to the file. Preferably in a C++ solution?
myFile.open("somefile.txt", ios::app);
myfile<<getc(fd);
How do I write the return value from getchar() function to a file? I tried the following and it wrote integers to the file. Preferably in a C++ solution?
myFile.open("somefile.txt", ios::app);
myfile<<getc(fd);
It may seem counter-intuitive, but getc
does not return a char
- it returns an int
. This is so that there's an out-of-range value to use to indicate end of file.
The behavior of <<
is vastly different between a char
and an int
. For char
it puts out the character as you'd expect, but for int
it outputs the decimal representation of a number.