Removing empty lines

Discussion in 'C Programming' started by happytoday, Jul 21, 2011.

  1. happytoday

    happytoday Guest

    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 .
     
    happytoday, Jul 21, 2011
    #1
    1. Advertising

  2. happytoday

    John Gordon Guest

    In <> happytoday <> 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
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Jul 21, 2011
    #2
    1. Advertising

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

    --
     
    Alexander Bartolich, Jul 21, 2011
    #3
  4. 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 }

    --
     
    Alexander Bartolich, Jul 21, 2011
    #4
  5. happytoday <> 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) <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"
     
    Keith Thompson, Jul 22, 2011
    #5
  6. 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?
     
    J. J. Farrell, Jul 22, 2011
    #6
  7. happytoday

    osmium Guest

    "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.
     
    osmium, Jul 22, 2011
    #7
  8. happytoday

    Eric Sosman Guest

    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
    d
     
    Eric Sosman, Jul 22, 2011
    #8
  9. In article <>,
    osmium <> 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...
     
    Kenny McCormack, Jul 22, 2011
    #9
  10. On Jul 21, 10: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 .
    >

    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
     
    Malcolm McLean, Jul 22, 2011
    #10
  11. On Jul 21, 10:29 pm, Malcolm McLean <>
    wrote:
    > On Jul 21, 10:35 pm, happytoday <> 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
     
    Anand Hariharan, Jul 22, 2011
    #11
  12. happytoday

    mt Guest

    On Jul 21, 8:07 pm, Keith Thompson <> wrote:
    > happytoday <> 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)  <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
     
    mt, Jul 22, 2011
    #12
  13. happytoday

    BartC Guest

    "happytoday" <> wrote in message
    news:...
    > 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
     
    BartC, Jul 27, 2011
    #13
  14. happytoday

    tm Guest

    On 22 Jul., 03:30, (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.
     
    tm, Jul 27, 2011
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott Taylor
    Replies:
    2
    Views:
    1,114
    Eric J. Roode
    Aug 4, 2003
  2. Ned D Hanks
    Replies:
    0
    Views:
    933
    Ned D Hanks
    Aug 4, 2003
  3. Replies:
    0
    Views:
    478
  4. shank

    removing top lines

    shank, Jan 28, 2004, in forum: HTML
    Replies:
    4
    Views:
    394
    Mark Parnell
    Jan 28, 2004
  5. Chris  Chiasson
    Replies:
    6
    Views:
    652
    Richard Tobin
    Nov 14, 2006
Loading...

Share This Page