Delete Blank Lines

Delete Blank Lines

Post by Mung Laam Ts » Thu, 09 Apr 1992 22:54:38



Hi Net-landers,

I am trying to write a C program in order to remove
all the empty lines in a text file. The following piece
of code is my tentative try, but it doesn't work at
all . Can anybody help me out. I'm not interested in
using UNIX utilities like awk, sed, .....

#include <stdio.h>
main(int argv, char *argv[])
{
    int c;
    FILE *fp;

    fp = fopen(argv[1], "r");
    while((c = getc(fp)) != EOF)
           if(c != '\n\n')
               putchar(c);
           else
                         ;

Quote:}

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

                             Philips/VSDM
                             IMEC
                             Kapeldreef 75
                             3000 Leuven
                             Belgium
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
 
 

Delete Blank Lines

Post by Tony Jon » Fri, 10 Apr 1992 04:06:16


: Hi Net-landers,
:
: I am trying to write a C program in order to remove
: all the empty lines in a text file. The following piece
: of code is my tentative try, but it doesn't work at
: all . Can anybody help me out. I'm not interested in
: using UNIX utilities like awk, sed, .....
:
: #include <stdio.h>
: main(int argv, char *argv[])
: {
:     int c;
:     FILE *fp;
:
:     fp = fopen(argv[1], "r");
:     while((c = getc(fp)) != EOF)
:          if(c != '\n\n')
:                putchar(c);
:            else
:                          ;
: }

first: change "int argv" to "int argc"

second: '\n\n' is two chars.
        You need to output the \n for normal lines, just not for 0 length
        lines.

The following works, 100% quick hack
#include <stdio.h>
main(int argc, char *argv[])
{
    int c, flag=0;
    FILE *fp;

    fp = fopen(argv[1], "r");
    while((c = getc(fp)) != EOF)
           if(c != '\n'){
                flag=0;
                putchar(c);
           }else if (!flag){
                        putchar('\n');
                        ++flag;
           }

Quote:}

tony
--

ComMon Compiler System
Cray Research Inc, 655F Lone Oak Road, Eagan, MN 55121

 
 
 

Delete Blank Lines

Post by Tony Jon » Fri, 10 Apr 1992 04:13:29


before someone mentions it:
"int c, flag=0;" should of course be "int c, flag=1;"

tony
--

ComMon Compiler System
Cray Research Inc, 655F Lone Oak Road, Eagan, MN 55121

 
 
 

Delete Blank Lines

Post by Tom Christianse » Sat, 11 Apr 1992 03:03:44



:Hi Net-landers,
:
:I am trying to write a C program in order to remove
:all the empty lines in a text file. The following piece
:of code is my tentative try, but it doesn't work at
:all . Can anybody help me out. I'm not interested in
:using UNIX utilities like awk, sed, .....

If you don't want to use UNIX, why are you posting
to comp.unix.questions?

--tom