Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Removing empty lines

Reply
Thread Tools

Removing empty lines

 
 
Anand Hariharan
Guest
Posts: n/a
 
      07-22-2011
On Jul 21, 10:29*pm, Malcolm McLean <malcolm.mcle...@btinternet.com>
wrote:
> On Jul 21, 10:35*pm, happytoday <ehabaziz2...@gmail.com> wrote:> It wasrequired to remove empty lines using C program . Either to be
> > run like :

>
> > ./nnoemptylines < textfile

>
> > or accept location of the file as a full path from the user dialog to
> > accept .

>
> The spec is almost impossible.
>


FWIW, while the point you make below is valid, I doubt if that is what
the OP intended.



> ./noemptylines < textfile redirects textfile to stdin. So if the
> program is invoked thusly, it needs to read in lines from stdin, check
> if they are non-blank, and then echo them to stdout. So far so good.
> The problem is that
> ./noemptyfiles
>
> should set up a dialogue with the user. So you want to print something
> like
> "Hello user, please enter the name of the file from which ypu wish to
> remove blanks"
> The user enters
> textfile
> "Thank you, do you wish to overwrite the file?"
>
> etc
>
> The problem is that there's no easy way to know whether stdin is
> directed from a file or coming from a keyboard. That's deliberate. We
> don't generally want programs making this distinction. I'm sure that
> on your particular system there will besome operating call, probably
> poorly documented, which you can make. But it's inappropriate and bad
> practice to use it for a ultility program like a deblanker.
>


POSIX provides a 'isatty()' that allows programs to make such a
distinction.


- Anand
 
Reply With Quote
 
 
 
 
mt
Guest
Posts: n/a
 
      07-22-2011
On Jul 21, 8:07*pm, Keith Thompson <ks...@mib.org> wrote:
> happytoday <ehabaziz2...@gmail.com> writes:
> > It was required to remove empty lines using C program . Either to be
> > run like :

>
> > ./nnoemptylines < textfile

>
> > or accept location of the file as a full path from the user dialog to
> > accept .

>
> One common answer to questions like this is to ask the questioner
> to supply the e-mail address of his or her instructor, so we can
> submit our solutions directly.
>
> --
> Keith Thompson (The_Other_Keith) ks...@mib.org *<http://www.ghoti.net/~kst>
> Nokia
> "We must do something. *This is something. *Therefore, we must do this."
> * * -- Antony Jay and Jonathan Lynn, "Yes Minister"


Haha... I like that
 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      07-27-2011


"happytoday" <> wrote in message
news:dd3fce00-b8a2-4758-9184-...
> It was required to remove empty lines using C program . Either to be
> run like :
>
> ./nnoemptylines < textfile
>
>
> or accept location of the file as a full path from the user dialog to
> accept .


Haven't written any C for a while, tried this:

#include <stdio.h>
#include <stdlib.h>

int main(int nargs, char** args) {
FILE *infile,*outfile;
int c,lastc='\n';

if (nargs<2) {
infile=stdin;
puts("(Reading from stdin)");
}
else {
infile=fopen(args[1],"r");
if (infile==0) {
printf("Can't find %s\n",args[1]);
exit(0);
}
printf("(Reading from %s\n",args[1]);
}
outfile=stdout;

while(1) {
c=fgetc(infile);
if (c==EOF) break;
if (c!='\n' || lastc!='\n') fputc(c,outfile);
lastc=c;
}

if (infile==stdin) fclose(infile);
if (outfile==stdout) fclose(outfile);

}

This sends output to stdout, rather than overwrite the input file (which is
tricky to do, as well as being a bad idea unless a backup scheme is in
place).

--
Bartc

 
Reply With Quote
 
tm
Guest
Posts: n/a
 
      07-27-2011
On 22 Jul., 03:30, gaze...@shell.xmission.com (Kenny McCormack) wrote:
> And when you do post code, it (*) will just get worse.
>
> (*) The abuse and misleading advice.
>
> --
> (This discussion group is about C, ...)
>
> Wrong. *It is only OCCASIONALLY a discussion group
> about C; mostly, like most "discussion" groups, it is
> off-topic Rorsharch [sic] revelations of the childhood
> traumas of the participants...


You really hit the spot.

I could easily proof your theory by suggesting Seed7.


Greetings Thomas Mertes

--
Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing GPO setting from XP machine after removing from Domain Piet Slaghekke Computer Support 4 01-02-2007 08:58 PM
removing a namespace prefix and removing all attributes not in that same prefix Chris Chiasson XML 6 11-14-2006 05:08 PM
removing replacing blank lines in ASP.NET (VB) jason@cyberpine.com ASP .Net 0 05-04-2004 03:36 PM
Re: newbie question about removing un needed extra lines Scott Taylor Perl 2 08-04-2003 10:04 AM
Re: newbie question about removing un needed extra lines Ned D Hanks Perl 0 08-04-2003 03:30 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57