Google Answers Logo
View Question
 
Q: C Code for text file output with word wrap ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: C Code for text file output with word wrap
Category: Computers > Programming
Asked by: flipper_01-ga
List Price: $10.00
Posted: 11 Dec 2002 07:51 PST
Expires: 10 Jan 2003 07:51 PST
Question ID: 123026
I'm looking for C code that will load a text ascii format file and
output it to another text file (user defined) with the users defined
amount of characters and not allow wrap around, ie if a word will not
fit on one line it should be placed on the next.

Flip.

Request for Question Clarification by hammer-ga on 11 Dec 2002 10:46 PST
Flip,

I am interested in answering your question, but I believe that, to
answer it well, your question will require more time and effort than
the average amount of time and effort associated with $6.00. Here is a
link to guidelines about pricing your question,
  
https://answers.google.com/answers/pricing.html 

If you both significantly raise your price and also post a
clarification here, the system will notify me and I will take another
look at your question.
- Hammer

Request for Question Clarification by wengland-ga on 11 Dec 2002 15:26 PST
Would Perl work, or does it require C code?

Clarification of Question by flipper_01-ga on 12 Dec 2002 04:48 PST
Hello,

The basic i/o file functions are okay with me, but assigning an array
and using the ability to word wrap is the main sticking point. I have
the basic ideal of getting the information into a string or array and
counting the number of characters, but dont know how to impiment it.
Answer  
Subject: Re: C Code for text file output with word wrap
Answered By: leapinglizard-ga on 12 Dec 2002 11:06 PST
Rated:5 out of 5 stars
 
I've written a program that takes three arguments, the first two of
which are the input file and output file, respectively, and the last
of which is the greatest number of characters you can allow in a line.
Code follows.

/* begin wrap.c */
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>

#define MAXLINE 10000
#define MAXWORD 1000

int main(int argc, char* argv[]) {
    FILE *inf, *outf;
    char c = '\0', cc, d, line[MAXLINE+2], word[MAXWORD+2];
    int width, newline = 0, line_pos = 0, word_pos = 0;
    if (argc != 4) {
        printf("USAGE: wrap <read_from> <write_to>
<chars_per_line>\n");
        return 0;
    }
    if (!strcmp(argv[1], argv[2])) {
        printf("ERROR: input and output files must differ\n");
        return 0;
    }
    if (!(inf = fopen(argv[1], "r"))) {
        printf("ERROR: can't read from \"%s\"\n", argv[1]);
        return 0;
    }
    if (!(outf = fopen(argv[2], "w"))) {
        printf("ERROR: can't write to \"%s\"\n", argv[2]);
        return 0;
    }
    if ((width = atoi(argv[3])) > MAXLINE || width < 1) {
        printf("ERROR: line width must be from 1 to %d, inclusive\n",
MAXLINE);
        return 0;
    }
    while (1) {
        d = cc;
        c = cc = getc(inf);
        if (c == EOF) {
            if (line_pos > 0) {
                line[line_pos] = '\0';
                fprintf(outf, "%s\n", line);
            }
            goto quit;
        }
        if (c == '\r')
            continue;
        if (c == '\n') {
            if (d == '\n')
                newline = 1;
            else
                c = ' ';
        }
        line[line_pos++] = word[word_pos++] = c;
        if (isspace(c))
            word_pos = 0;
        if (word_pos > MAXWORD) {
            word[word_pos] = '\0';
            printf("ERROR: \"%s\" exceeds maximum word length (%d)\n",
word, MAXWORD);
            goto quit;
        }
        if (newline || line_pos > width) {
            newline = 0;
            line_pos -= word_pos;
            while (isspace(line[--line_pos]) && line[line_pos] !=
'\n');
            line[line_pos+1] = '\0';
            fprintf(outf, "%s\n", line);
            strncpy(line, word, word_pos);
            line_pos = word_pos;
        }
    }
    quit:;
    fclose(outf);
    fclose(inf);
    return 0;
}
/* end wrap.c */

Please note that my program counts the tab character ('\t') as a
single character, although different editors will display it in
different ways. Some will expand it to four spaces, others to eight,
and so on. You shouldn't have any trouble with files that contain no
tabs.

Also note that the carriage return character ('\r') is always
discarded.

If you have any difficulty compiling or running this program, please
let me know so that I have a chance to meet your needs before you rate
my answer.

Cheers,

leapinglizard
flipper_01-ga rated this answer:5 out of 5 stars and gave an additional tip of: $2.00
very good and easy to understand code - very good, well worth the money!

Comments  
Subject: Re: C Code for text file output with word wrap
From: wengland-ga on 11 Dec 2002 15:39 PST
 
Basics of wordwrap are in the code in this Usenet article, posted to
comp.lang.c on Wed, 16 Nov 1994.  It has a lot of unneeded bits for
this, but it should be simple for another researcher to cut the key
points out, add in the fopen() and write() calls, throw in some error
checking and there ya go.

http://groups.google.com/groups?selm=785176427.AA08794%40rochgte.fidonet.org&output=gplain

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy