//* The idea of a log file (appending) *

#ifdef _MSC_VER // for MS Visual Studio compilers - to allow "classic" insecure printf and scanf #define _CRT_SECURE_NO_WARNINGS #endif #include <stdio.h> int main() { const char LogFileName[] = "log.txt"; FILE *logfile; char name[80]; printf("Hello to the guest book program,\n\nWhat is your name please? "); scanf(" %79[^\n]s\n", name); logfile = fopen(LogFileName, "a"); // a - append (write at the end, create if does not exist) if (logfile!=0) { fprintf(logfile,"%s\n", name); fclose(logfile); } else { printf("Error appending to the guestbook file.\n"); } // in typical logging situation we are not interested in reopening the file for reading logfile = fopen(LogFileName, "r"); if (logfile!=0) { printf("\n\nThe following guests run this program:\n"); for (;;) { fgets(name, 80, logfile); // remember, it does read and put \n too if (feof(logfile)) break; printf("%s", name); } fclose(logfile); } else { printf("Error opening the guestbook file.\n"); } return(0); }