Sequential Files (Ch. 28)

Warning — Our authors gives this line of code:
FILE * fptr = fopen("c:\cprograms\cdata.txt", "w");
It will, unfortunately, compile (with a warning: unknown escape sequence: '\c') and it will run successfully  — if successfully is defined as not crashing.  It will, however, not open the file expected.  It will instead open a file named cprogramscdata.txt in the current default directory (which happens to be on the C: drive).

Remember that the backslash in a string starts an escape sequence, and the escape sequence for the backslash is \\.  Hence it SHOULD read
FILE * fptr = fopen("c:\\cprograms\\cdata.txt", "w");
if you want to give the file name as a quoted string.

C accesses files through the function (in stdio.h) FILE * fopen(char *name, char *access).  Notice that it returns a pointer.  The first character string argument gives the name of the file.  If it is not an absolute path (as our authors use in their example), it will be a path relative to the default directory in which the program is executing.  The second character string argument is the type of access requested.

If the fopen operation fails, it returns a zero, the NULL pointer.  Otherwise it returns the address of the data structure providing access to the file.  So, just in case, you may want to do this:

fptr = fopen(filename, "r");
if (fptr == 0)
{  printf("Opened failed on %s.  Abort.\n", filename);
Since we will start with input files, this is an example of opening a file for reading.

The character string specifying access recognizes three different modes of access:

Note that fopen may fail because you do not have the file access required.  If, for instance, the file you name is in the directory as a "read-only" file, the open attempt will fail for both "w" and "a".

Demonstration of opening for read:
DemoRead.txt   DemoRead.c   DemoRead.exe

Demonstration of opening for write:
DemoWrite.txt   DemoWrite.c   DemoWrite.exe

Note on output files:  when dealing with writing to disk files, operating systems save up the information to be written to the disk and periodically write to the disk.  Consequently you must be sure that you expressly close any file that you open for writing — or you may find that not everything made it into the file.  Hence every fopen should be matched with an fclose, as seen in the two demonstration programs above.

Page on file I/O functions.

Exercise:  Copy/paste the following code into a file for your C compiler.  Then compile and execute it.  The first time, let it run to completion.  The second time, interrupt it in progress — ^C or simply close the execution window.

#include <stdio.h>

int main()
   FILE * fptr = fopen("demoOut.txt", "w");
   char buffer[256];
   int  k;

   for (k = 0; k < 6; k++)
      printf("Line for file:  ");
      fprintf(fptr, "%d:  %s\n", k, buffer);
//    To insure the data goes to the file, enable the next line.
//    fflush(fptr);

Further example:  encryption program in which the same program encrypts and decrypts text.
One on-line method of information sharing that has been around for decades is the use of Usenet news groups.  They reside on new servers and are accessed through a program called a newsreader.  Within the Usenet community there arose a method to shield the casual reader from potentially offensive posts, but to make the contents easily accessible to those who wish to access it.  This is to shift all alphabetic characters by 13 positions in the alphabet.  The first pass generates text that is basically illegible, but has a distinctive feel.  The second pass rotates by another 13 positions, meaning that the character moves back to itself.

In this fashion someone reading a newsgroup post cannot complain about offensive content:  the user was warned of potentially offensive material and chose to read it anyway.

This is a fairly easy program to write and demonstrates single character file I/O as well as the isalpha, and the islower or isupper functions.
rot13.txt   rot13.c   rot13.exe