So basically I want to copy everything i write to stdin (including newline char) to string for hash purposes. I managed to accomplish that and made small code to represent my problem.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFFERSIZE 10000
int main()
{
char *myStr = calloc(1,1);
char buffer[BUFFERSIZE];
while( fgets(buffer, BUFFERSIZE , stdin) != NULL ){
myStr = realloc(myStr, strlen(myStr)+1+strlen(buffer) );
strcat( myStr, buffer );
}
printf("\n%s\n",myStr);
}
everything works when I enter some text then press ENTER and after I call EOF.
But when I start program enter "a" then I try to call EOF (using Ctrl Z + ⏎ (Windows cmd prompt), Ctrl D (Linux)) I have to do it three times for program to actually break the loop. I was expecting maximum of 2 times.
Can someone explain how using EOF, stdin and fgets works? Or should I use something else (for example getline)? I am sorry if I am not clear about my problem, just ask anything you need.
Thank you.
First of all, ^Z or ^D are control characters that mean something to the terminal you are using, and sometimes that means for the terminal to signal end-of-file condition.
Anyway, your three keypresses are processed by the terminal to take the following actions, after entering text:
Inside your program that corresponds to:
a
is received,fgets
keeps reading until end-of-file or newlinefgets
completes because of end-of file. However it does not return NULL because characters were read,"a"
to be specific.fgets
completes because of end-of-file, and returnsNULL
because there were no characters read.