Re: code won't work

Discussion in 'C Programming' started by Emmanuel Delahaye, Jun 24, 2003.

  1. In 'comp.lang.c', (Tony Brien) wrote:

    > hi guys
    >
    > have a problem with this c code. i tried the program and compiled it
    > and worked so far, now i tried to change the -s argument so that vis
    > -sn will print onlystrings n or more consecutive printable characters,
    > discarding non-printing characters and short sequences of printable
    > ones.
    >
    > this is for isolating the text parts of non-text files such as
    > executable programs.
    >
    > here the code so far:


    Missing prototypes:

    #include <stdio.h>
    #include <ctype.h>

    > main ()


    int main ()

    > {
    > int c, l;
    >
    > while ((c = getchar()) != EOF)
    > if (isascii(c) &&


    isascii() is not standard.

    > (isprint(c) || c=='\n' || c=='\t' || c==' '))


    isspace() can help...

    > if (l < 65) {


    Better to give sense to this constant (MAXCOL or the like)

    > putchar(c);
    > l = l + 1;


    l++;

    Beware, 'l' was not initialized. You want

    int l = 0;

    before the loop.


    > } else {
    > putchar('\n');
    > putchar(c);
    > l = 0;
    > }


    I see, it's a kind of simple text formatter.

    > else
    > printf("\\%03o", c);


    "%o" wants an unsigned int.

    printf("\\%03o", (unsigned) c);

    > exit(0);


    No need for that.

    return 0;

    is fine.

    > }


    Also, your indentation is... ugly!

    This code works fine:

    #include <stdlib.h>

    #include <stdio.h>
    #include <ctype.h>

    enum
    {
    MAXCOL = 65,

    dummy
    };

    int main (void)
    {
    int c;
    int col = 0;

    while ((c = getchar ()) != EOF)
    {
    if (isprint (c) || isspace(c))
    {
    if (col < MAXCOL)
    {
    putchar (c);
    col++;
    }
    else
    {
    putchar ('\n');
    putchar (c);
    col = 0;
    }
    }
    else
    {
    printf ("\\%03o", (unsigned) c);
    }
    }
    system ("pause");

    return 0;
    }

    Bonjour, voici un texte en français avec des caractères accentués.
    Bonjour, voici un texte en fran\207ais avec des caract\212res accentu\202s.


    Hi, this is a long text in English that is supposed to demonstrate that this
    software is working correctly.
    Hi, this is a long text in English that is supposed to demonstrat
    e that this software is working correctly.

    The next step should be to cut the words properly, or to add an hyphen at the
    right place...

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-library: http://www.dinkumware.com/htm_cl/index.html
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
    Emmanuel Delahaye, Jun 24, 2003
    #1
    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. Chad
    Replies:
    4
    Views:
    8,331
  2. learningjava
    Replies:
    8
    Views:
    477
    John C. Bollinger
    Dec 12, 2003
  3. Joona I Palaste

    Re: Why won't this code work?

    Joona I Palaste, Aug 19, 2003, in forum: C Programming
    Replies:
    1
    Views:
    347
    Joona I Palaste
    Aug 19, 2003
  4. John Bode

    Re: Why won't this code work?

    John Bode, Aug 20, 2003, in forum: C Programming
    Replies:
    2
    Views:
    363
    Dave Thompson
    Sep 1, 2003
  5. Felipe Ribeiro
    Replies:
    20
    Views:
    1,995
    Richard Bos
    Jul 5, 2009
Loading...

Share This Page