Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

line wrapping [faggotry]

Name: Anonymous 2008-03-08 12:53

Make a simple line wrapping program


#include <stdio.h>
#include <string.h>

void format(const char *str, size_t len, FILE *out);

int main(int argc, char *argv[])
{

    char buf[BUFSIZ];
    size_t len;

    if(argc != 2) return 0;
    len = atoi(argv[1]);

    while (fgets(buf, sizeof buf, stdin) != NULL) {
    format(buf, len, stdout);
    }


    return 0;
}

void format(const char *str, size_t sz, FILE * fp)
{

    size_t n;
    const char *p;

    n = strlen(str);
    if (n <= sz) {
    fputs(str, fp);
    putc('\n', fp);
    } else
    while (n > sz) {
        p = str + sz;
        while (p != str) {
        if (*p == ' ')
            break;
        p--;
        }
        if (p == str) {
        fwrite(str, 1, sz, fp);
        putc('\n', fp);
        n -= sz;
        str += sz;
        } else {
        fwrite(str, 1, p - str, fp);
        putc('\n', fp);
        p++;
        n -= p - str;
        str = p;
        }
    }

    if (n != 0) {
    fwrite(str, 1, n, fp);
    putc('\n', fp);
    }
}


Example input/output:

$ gcc wrap.c
$ ./a.out 3
hello world
hel
lo
wor
ld

Name: Anonymous 2008-03-08 13:04

try indenting faggot

Name: Anonymous 2008-03-08 13:07

>>2
FORCED INDENTATION OF CODE

Name: Anonymous 2008-03-08 13:10

>>1
FORCED WRAPPING OF LINES

Name: Anonymous 2008-03-08 13:14

f x n=mapM_ print$concat$map(unfoldr((>>).guard.not.null<*>return.splitAt n))$words x

Name: Anonymous 2008-03-08 13:19

>>5
simon peyote joints

Name: Anonymous 2008-03-08 13:22

M-x fill-mode

Name: Anonymous 2008-03-08 13:23

>>6
I invented that meme.

Name: Anonymous 2008-03-08 13:28

>>5
The long funny button, at the bottom of your keyboard, please befriend it.

Name: Anonymous 2008-03-08 13:36


ben@~: echo "hello world" | fold -w 3
hel
lo
wor
ld

Name: Anonymous 2008-03-08 13:49

>>10
One word, the forced re-invention of the wheel.  Thread over.

Name: Anonymous 2008-03-08 14:03

Jesus Christ, would you please stop shouting your memes right and left? It is neither funny nor adds anything relevant to the discussion.

Name: Anonymous 2008-03-08 14:07

>>12
back to /b/, please

Name: Anonymous 2008-03-08 15:13

>>12
This may surprise you, but I invented the ``stop shouting your memes'' meme.

Name: Anonymous 2008-03-08 15:22

I invented the back to /b/, please in code tags meme

Name: Anonymous 2008-03-08 15:51

I invented the ``I invented the meme'' meme.

Name: Anonymous 2008-03-08 15:55

I invented the ``I invented the ``I invented the meme'' meme'' meme.

Name: Anonymous 2008-03-08 16:17

I popularized the ``I invented the meme'' meme.

Name: Anonymous 2008-03-08 16:26

I shouted the "I invented the" meme.

Name: Anonymous 2008-03-08 16:31

>>19
back to /b/, please

Name: Anonymous 2008-03-11 7:22

say join "\n",map{/\S{1,3}/g} <>

Name: Anonymous 2008-03-12 0:13

okay.

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

void
format (FILE * read, FILE * write, size_t col_w)
{
    int ch;
    size_t n = 0;

    if (!read || !write) {
        puts("Your files are broken");
        exit(1);
    }
   
    while ((ch = fgetc(read)) != EOF) {
        if (n++ == col_w) {
            n = 0;
            ungetc(ch, read);
            fputc('\n', write);
            continue;
        } else {
            fputc(ch, write);
        }
    }
    return ;
}

int
main (const int argc, const char * const *argv)
{
    FILE * read, * write;

    if (argc == 3) {
        read = fopen(argv[1], "r");
        if (!read) {
            perror(argv[1]);
            return 0;
        }
        write = fopen(argv[2], "w");
        if (!write) {
            perror(argv[2]);
            return 0;
        }
        format(read, write, strtoul(argv[3], NULL, 10));
        fclose(read);
        fclose(write);
    } else if (argc == 2) {
        format(stdin, stdout, strtoul(argv[1], NULL, 10));
    } else {
        puts("Usage: fold [<input-file> <output-file>] <column width>");
        puts("Fold standard input to a specific column width, and dump it to standard output.");
    }
    return 0;
}


hello world
hel
lo
wor
ld

Name: Anonymous 2008-03-12 0:53

OP here,
>>22
Fail.
re-read my code and try to understand what *my* code does and what yours does.

# correct
$ ./format 8
hello world
hello
world

# wrong
$ ./format 8
hello world
hello wo
rld

Name: Anonymous 2008-03-12 3:02

I think my code is just fine thx.  That was not in the spec.

Name: Anonymous 2011-02-04 12:23


Don't change these.
Name: Email:
Entire Thread Thread List