Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Removing empty lines

 
 
happytoday
Guest
Posts: n/a
 
      07-21-2011
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 .


 
Reply With Quote
 
 
 
 
John Gordon
Guest
Posts: n/a
 
      07-21-2011
In <(E-Mail Removed)> happytoday <(E-Mail Removed)> 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 .


You haven't actually asked a question. What do you want?

--
John Gordon A is for Amy, who fell down the stairs
http://www.velocityreviews.com/forums/(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
 
 
 
Alexander Bartolich
Guest
Posts: n/a
 
      07-21-2011
happytoday wrote:
> 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 .


1 #include <unistd.h>
2 int main()
3 {
4 extern char **environ;
5 static char* const argv[3] = { "/bin/sed", "/^$/d", 0 };
6 return execve(argv[0], argv, environ);
7 }

--
 
Reply With Quote
 
Alexander Bartolich
Guest
Posts: n/a
 
      07-21-2011
happytoday wrote:
> It was required to remove empty lines using C program . Either to be
> run like :
>
> ./nnoemptylines < textfile


$ nl -ba nnoemptylines.c
1 #include <unistd.h>
2 int main()
3 {
4 extern char **environ;
5 static char* const argv[3] = { "/bin/sed", "/^$/d", 0 };
6 return execve(argv[0], argv, environ);
7 }

--
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-22-2011
happytoday <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
J. J. Farrell
Guest
Posts: n/a
 
      07-22-2011
happytoday wrote:
> 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 .


So? Do you have a question about C?
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      07-22-2011
"happytoday" wrote:

> 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 .


It is extremely unlikely that you will get any actual help until you post
code of an attempt, however misguided, that you have made at solving the
problem. Lacking that, you will simply get abuse and misleading advice.


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      07-22-2011
On 7/21/2011 3:35 PM, happytoday wrote:
> 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 essential ingredient will be a means to determine whether
a particular line is or is not empty. Starting with the observation
that a string is empty if and only if all its substrings are empty,
you could use something like

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isEmpty(const char *string) {
int all_are_empty = 1; /* optimistic start */
for (size_t p = 0; p < strlen(string); ++p) {
for (size_t q = p; q < strlen(string); ++q) {
char *temp = malloc(q - p + 1);
if (temp == NULL) {
fputs("Out of memory!\n", stderr);
abort();
}
memcpy(temp, string + p, q - p);
temp[q - p] = '\0';
all_are_empty &= isEmpty(temp);
free(temp);
}
}
return all_are_empty;
}

This solution is efficient because it's recursive.

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      07-22-2011
In article <(E-Mail Removed)>,
osmium <(E-Mail Removed)> wrote:
>"happytoday" wrote:
>
>> 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 .

>
>It is extremely unlikely that you will get any actual help until you post
>code of an attempt, however misguided, that you have made at solving the
>problem. Lacking that, you will simply get abuse and misleading advice.


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...

 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      07-22-2011
On Jul 21, 10:35*pm, happytoday <(E-Mail Removed)> wrote:
> 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 .
>

The spec is almost impossible.

../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.

I'd throw the spec back, with this objection.

--
Roll up, roll up, free source code
http://www.malcolmmclean.site11.com/www

 
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