Hi everyone,

Need a small help.

I need to read the data from stdin (standard input) without hitting enter. Need to read the data as in and when they are typed in until EOF (CTRL + D as I am using UNIX) is encountered.

I tried,

getc() - requires ENTER to be hit
getchar() - requires ENTER to be hit
getch() - not available in UNIX

Any suggestions would be of great help.

Thank You,
Regards,
Ahamed.

Got the answer,

char buffer[81];
int ch;
for(int i = 0; (i < 80) && ((ch = getchar()) != EOF) && (ch != '\n'); i++ )
      buffer[i] = (char)ch;
buffer[i] = '\0';
printf( "Input was: %s\n", buffer );

The above code will work.

Regards, Ahamed

How is that any different from calling fgets(buffer, sizeof buffer, stdin )

I didn't consider this fgets... This will also give the same effect... thank you... i think i will use fgets rather than the former one... this will optimize the code...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.