Take 2 of trying to remove blank lines

So I'm trying to get my program to be able to have a txt file piped though in shell, and just put out the non-empty lines for an assignment. I scrapped the first one that I was working on and this is take 2, but it comes up with a segment fault. I have to use #C so I can't use std::

#include <stdio.h>

int main( void )
char nline[5] = "\n\n";
char *a = nline;
char c;

while( ( c = getchar()) != EOF){
if( strcmp(a, c) == 0){
c = '\n';
putchar( c );
else {
putchar( c );

i've been work this out for a week now but I just have no idea. Any help would be greatly appreciated.
Your code has too many errors.

For this to work ( c = getchar() ) != EOF, c must be an int.

And what is this? strcmp(a, c)

In C, the simple way to read a line from stdin is to use fgets()

#include <stdio.h>

int main()
    enum { MAX_CHARS = 8192 } ;
    static char line[MAX_CHARS] ;

    while( fgets( line, MAX_LEN, stdin ) ) // for each line till eof
        if( line[0] != '\n' ) // if it is not an empty line
            printf( "%s", line) ; // print it out
I was so suprised to hear that C# does not provide an equivalent to std::getline() that I did consult the crystal ball known as websearch.


Edit: If the shell has access to grep, then
grep -v '^$' < infile > outfile
Last edited on
Awesome thank you so much for your help the above works great.
Topic archived. No new replies allowed.