From 481f76699e735feee502c1a80b910aca8e834d0d Mon Sep 17 00:00:00 2001 From: Reina Harrington-Affine Date: Tue, 2 Sep 2025 21:02:47 +0000 Subject: [PATCH] storage.txt is now created at runtime rather than being assumed to be present and empty. recall() now removes storage.txt at completion. main() now includes a check to see whether storage.txt already exists, sends an error message, and exits with code 1 if storage.txt is already present. --- KNR C/ex1-16-2.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/KNR C/ex1-16-2.c b/KNR C/ex1-16-2.c index 22b4879..b37445a 100644 --- a/KNR C/ex1-16-2.c +++ b/KNR C/ex1-16-2.c @@ -1,4 +1,6 @@ #include +#include +#include void store(); void enumerate(); @@ -7,6 +9,11 @@ int longest = 0; int posLongest = 0; int main(){ + if((access("storage.txt",F_OK)) == 0){ + printf("Error: storage file already exists."); + return 1; + EXIT_FAILURE; + } store(); enumerate(); recall(); @@ -40,14 +47,14 @@ void enumerate(){ if(c != '\n'){ ++length; continue; - } else if(c == '\n' && length > longest){ - longest = length; - length = 0; - posLongest = position; - ++position; - } else if (c == '\n' && length < longest){ - ++position; - length = 0; + } else if(c == '\n' && length > longest){ + longest = length; + length = 0; + posLongest = position; + ++position; + } else if (c == '\n' && length < longest){ + ++position; + length = 0; } fclose (fp); } @@ -72,4 +79,5 @@ void recall (){ } } fclose (fp); + remove("storage.txt"); }