I need to write a program that reads from stdin and only writes non-empty lines to stdout (i.e. lines that only contain \n). For example, if the stdin was:
1
2
\n
3
The output would be:
1
2
3
This is what I have so far:
#include <stdio.h>
#include <string.h>
int main()
{
char buf[BUFSIZ];
char *p;
printf ("Please enter some lines of text\n");
if (fgets(buf, sizeof(buf), stdin) != NULL)
{
printf ("%s\n", buf);
/*
* Remove newline character
*/
if ((p = strchr(buf, '\n')) != NULL)
*p = '\0';
}
return 0;
}
Is there any way that I can loop the program around though, so that even if a blank line is entered, the user can still keep inputting?
1 Answer