linux - How to replace multiple blank lines with a single line in C language -


i want squeeze multiple lines in single line. have tried apply own logic there wrong.

 char *p;                     linecount=0;                     while (fgets(buffer, sizeof(buffer), file))                     {                      //it print user input number                     p=buffer;                      if('\r' == *p ||'\n' == *p )                     {                          linecount++;                        if(linecount>2 )                         printf("\n");                     }                      else                     {                         linecount=0;                              printf("\t %s", p);                      } 

for instance file has lines like

a b   c   d e 

then output should be

a b  c  d e 

basically, devloping code cat -s command.

in if block:

if(linecount>2 )     printf("\n"); 

what doing printing out 3rd, 4th, ..nth blank lines.

the first blank have linecount = 1

the second blank have linecount = 2

i reverse logic have

if( linecount == 0 ){     linecount++;     printf("\n"); } 

this way, print out first blank line in list of newlines.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -