7 years ago.

How to read a stream of characters from the local file system

I want to read a text file that has been stored on the local file system. This is more or less random characters, so I'd like to be able to read the file one character at a time.

I found a couple simple examples of writing to the local file system, but not much that shows reading.

Also, how do I find a complete list of the functions that are available to read/write local file system files? The documentation for local file system doesn't list them.

1 Answer

Chuck Davis
poster
7 years ago.

After fumbling around a bit, I think I answered my own question, so if anyone else is interested, here's what I found:

After opening a file for input (read) access with:

FILE *fin = fopen("/local/in.txt", "r"); open file named "in.txt" int readchar; define a variable to hold a single character

Read each character with:

readchar = fgetc(fin); read a character

if (readchar==EOF) fclose(fin); if EOF is read, close the file

You can also write individual characters to an output stream with: fputc(char,fout); if fout has been opened for output ("w") Individual characters can be intermixed with other output from fprintf in the same stream if necessary.

I'm sure this is all very obvious to those with a lot of c++ experience, but for newbies like me everything is a mystery.