INF: After Edit, fopen with Mode "a" Doesn't Appear to Append

ID Number: Q50793

5.10 6.00 6.00a 6.00ax 7.00

MS-DOS

Summary:

If you use an editor such as Edlin or WordStar to edit a file, and you

call the fopen() routine with the "a" option and append it to the

file, the appended text does not appear when the file is TYPEd.

More Information:

Edlin and WordStar are examples of editors that place a CTRL+Z

character at the end of the file. Opening a file with the "a" option

writes to the file beginning after the last character in the file (in

this case, the CTRL+Z). If you then edit the file again with the same

editor, the editor ignores all characters after the CTRL+Z.

The MS-DOS TYPE command displays all characters up to the first EOF.

However, if you open the file in the M editor or in Microsoft Word,

which both display the CTRL+Z character as a normal character, you can

see the appended text and, if you want, delete the CTRL+Z character.

If you fopen a file with the "a+" mode, the CTRL+Z character is

deleted at the end of the file, and appended text can be seen in any

editor or by using the MS-DOS TYPE command.

The following code demonstrates this behavior:

#include <stdio.h>

#include <time.h>

void main(void)

{

FILE *fp;

int num;

time_t system_time;

fp = fopen("out.dat","a"); /* change to "a+" to fix */

time(&system_time); /* to demonstrate last append */

num = fprintf(fp, "Writing to file at %s\n",

ctime(&system_time));

num = fclose(fp);

}

Run this program once to write the current system time to the output

file. Then use Edlin to open "out.dat". You do not need to add any

lines, just open the file and then end the session. Run the program

again. Type "out.dat"; notice that you still see only the first output

time.

Changing the fopen "a" to "a+" and rebuilding the program will

demonstrate the solution.

Additional reference words: 5.10 6.00 6.00a 6.00ax 7.00