Skip to content

Instantly share code, notes, and snippets.

@knusbaum
Created June 10, 2015 01:50
Show Gist options
  • Save knusbaum/125f440bdc8566142cc1 to your computer and use it in GitHub Desktop.
Save knusbaum/125f440bdc8566142cc1 to your computer and use it in GitHub Desktop.
Final Guestbook
/* This program was written with time and love in 'ed'
I hope you enjoy playing with it as much as I enjoyed
writing it. */
#include <stdio.h>
#define BUFFER_SIZE 50
int main() {
FILE * gbook;
char bytes[BUFFER_SIZE];
int read;
long position = 0;
int i;
gbook = fopen(".guestbook", "r");
while( (read = fread(bytes, 1, BUFFER_SIZE - 1, gbook)) > 0 ) {
bytes[read] = 0;
printf("%s", bytes);
}
fclose(gbook);
gbook = fopen(".guestbook", "a");
printf("Sign the guestbook! (Or Backspace to quit)\n");
printf("Your Name: ");
for(i = 0; i < BUFFER_SIZE; i++) {
bytes[i] = 0;
}
while( (read = getchar()) != '\n') {
bytes[position] = read;
position = (position + 1) % BUFFER_SIZE;
}
fprintf(gbook, "%s >\n", bytes);
printf("Your Message (Ctrl-D to end): \n");
fprintf(gbook, "\t");
while( (read = getchar()) != EOF) {
fprintf(gbook, "%c", read);
if(read == '\n') {
fprintf(gbook, "\t");
}
}
fprintf(gbook, "\n");
printf("Thanks!\n");
fclose(gbook);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment