1
0

addaword.c 824 B

1234567891011121314151617181920212223242526272829303132
  1. /* addaword.c -- uses fprintf(), fscanf(), and rewind() */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define MAX 41
  6. int main(void)
  7. {
  8. FILE *fp;
  9. char words[MAX];
  10. if ((fp = fopen("wordy", "a+")) == NULL)
  11. {
  12. fprintf(stdout,"Can't open \"wordy\" file.\n");
  13. exit(EXIT_FAILURE);
  14. }
  15. puts("Enter words to add to the file; press the #");
  16. puts("key at the beginning of a line to terminate.");
  17. while ((fscanf(stdin,"%40s", words) == 1) && (words[0] != '#'))
  18. fprintf(fp, "%s\n", words);
  19. puts("File contents:");
  20. rewind(fp); /* go back to beginning of file */
  21. while (fscanf(fp,"%s",words) == 1)
  22. puts(words);
  23. puts("Done!");
  24. if (fclose(fp) != 0)
  25. fprintf(stderr,"Error closing file\n");
  26. return 0;
  27. }