[Info-vax] ChatGPT solved it for me, again...

Arne Vajhøj arne at vajhoej.dk
Fri Feb 17 09:55:32 EST 2023


On 2/17/2023 9:45 AM, Slo wrote:
> Me: But fgets() is not guaranteed to return a newline character. Can you improve the code?
> ==========================
> You're correct that fgets() may not return the newline character if the
> input buffer is filled before encountering a newline. Here's an updated
> version of the code that handles this case:

The code did not have a problem with missing newline.

The code did not handle errors or empty input.

>      if (fgets(sys1, 256, stdin) == NULL) {
>          perror("fgets failed");
>          exit(1);
>      }

>      if (sys1[0] == '\0') {
>          fprintf(stderr, "Invalid input: system name cannot be empty\n");
>          exit(1);
>      }

>      if (fgets(sys2, 256, stdin) == NULL) {
>          perror("fgets failed");
>          exit(1);
>      }

>      if (sys2[0] == '\0') {
>          fprintf(stderr, "Invalid input: system name cannot be empty\n");
>          exit(1);
>      }

But neither did the Pascal code.

Arne





More information about the Info-vax mailing list