N
name
Here is a first attempt at a line/word wrapping utility. Seems to work
okay, but lacks some checking stuff, etc.
---------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define MAX 10000
/* wrap.c inserts newlines in place of spaces according to specified
line length. Output filename is {filename}.wrap. Takes two arguments,
filename and line length. */
/* Todo: Need to figure out what sort of memory
the larger files might need. File type checking? */
void wordwrap(FILE *ifp, FILE *ofp, char *wl)
{
int c;
char buf[MAX];
int i, space, count, length;
i = space = count = 0;
length = atoi(wl);
for(i = 0; i < MAX && ((c=getc(ifp)) != EOF); ++i)
{
buf = c;
}
for ( i = 0; buf != '\0'; ++i)
{
if ((buf == '\n' || buf == '\t') && buf[i-1] == '\n')
count = space = 0;
if ( buf == ' ')
space = i;
++count;
if ( count == length )
{
buf[space] = '\n';
count = i - space;
}
}
for ( i = 0; buf != EOF; ++i)
{
c = buf;
putc(c, ofp);
}
}
int main(int argc, char *argv[])
{
FILE *fp1;
FILE *fp2;
char *prog = argv[0];
char *filename1 = argv[1];
char filename2[80];
char *wl = argv[2];
int i, l;
l = strlen(argv[1]);
for ( i = 0; i < 75 || i < l; ++i)
filename2 = argv[1];
strcat(filename2, ".wrap");
printf("Wrapping %s at %s\n", filename1, wl);
printf("Output file adds .wrap to input filename.\n");
if (argc != 3)
{
printf("Usage: %s: filename, wrap length\n", prog);
return EXIT_FAILURE;
}
else if ((fp1 = fopen(filename1, "r")) == NULL)
{
fprintf(stderr, "%s: can't open %s\n", prog, filename1);
return EXIT_FAILURE;
}
else if ((fp2 = fopen(filename2, "w")) == NULL)
{
fprintf(stderr, "%s: can't open %s\n", prog, filename2);
return EXIT_FAILURE;
}
else
{
wordwrap(fp1, fp2, wl);
fclose(fp1);
fclose(fp2);
}
if (ferror(fp2))
{
fprintf(stderr, "%s: error writing %s\n", prog, argv[3]);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
okay, but lacks some checking stuff, etc.
---------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define MAX 10000
/* wrap.c inserts newlines in place of spaces according to specified
line length. Output filename is {filename}.wrap. Takes two arguments,
filename and line length. */
/* Todo: Need to figure out what sort of memory
the larger files might need. File type checking? */
void wordwrap(FILE *ifp, FILE *ofp, char *wl)
{
int c;
char buf[MAX];
int i, space, count, length;
i = space = count = 0;
length = atoi(wl);
for(i = 0; i < MAX && ((c=getc(ifp)) != EOF); ++i)
{
buf = c;
}
for ( i = 0; buf != '\0'; ++i)
{
if ((buf == '\n' || buf == '\t') && buf[i-1] == '\n')
count = space = 0;
if ( buf == ' ')
space = i;
++count;
if ( count == length )
{
buf[space] = '\n';
count = i - space;
}
}
for ( i = 0; buf != EOF; ++i)
{
c = buf;
putc(c, ofp);
}
}
int main(int argc, char *argv[])
{
FILE *fp1;
FILE *fp2;
char *prog = argv[0];
char *filename1 = argv[1];
char filename2[80];
char *wl = argv[2];
int i, l;
l = strlen(argv[1]);
for ( i = 0; i < 75 || i < l; ++i)
filename2 = argv[1];
strcat(filename2, ".wrap");
printf("Wrapping %s at %s\n", filename1, wl);
printf("Output file adds .wrap to input filename.\n");
if (argc != 3)
{
printf("Usage: %s: filename, wrap length\n", prog);
return EXIT_FAILURE;
}
else if ((fp1 = fopen(filename1, "r")) == NULL)
{
fprintf(stderr, "%s: can't open %s\n", prog, filename1);
return EXIT_FAILURE;
}
else if ((fp2 = fopen(filename2, "w")) == NULL)
{
fprintf(stderr, "%s: can't open %s\n", prog, filename2);
return EXIT_FAILURE;
}
else
{
wordwrap(fp1, fp2, wl);
fclose(fp1);
fclose(fp2);
}
if (ferror(fp2))
{
fprintf(stderr, "%s: error writing %s\n", prog, argv[3]);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}