File Open Modes< Back to Methodology | Forward to File Read Functions >
C provides two modes in which you can open files:
In binary mode, your program can access every byte in the file. When you use one of C's read functions, it "sees" the file exactly as it is on disk, character-for-character. Any file may be opened in binary mode.
The following code fragment will open the file afile.txt for reading in binary mode:
FILE *inputFilePtr; inputFilePtr = fopen("afile.txt", "rb");
The "r" in the second argument to fopen() opens the file for reading, and the "b" specifies binary mode.
Text mode is used only for text files. When a file is opened in text mode, some characters may be "hidden" from your program. The characters are still in the file, your program just can't "see" them. You can think of this as a "filter" between the file and your program.
The ANSI standard for C does not specify what filtering should take place when a file is opened in text mode; it's compiler-dependent. For compilers used in the DOS/Windows environment, a file opened in text mode will normally have its carriage-returns filtered out. This has the effect of translating the CR/LF pairs into single LFs. As the program output shows, different compilers implement this translation differently.
The following code fragment will open the file afile.txt for reading in text mode:
FILE *inputFilePtr; inputFilePtr = fopen("afile.txt", "r");
Note that the second argument to fopen() is just an "r" (without the "b" that specifies a binary open). Text mode is the default file open mode.< Back to Methodology | ^ Up to Top | Forward to File Read Functions >