N
name
Back again for more critique... <grin>
------------------------------------------------
#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.
Done: Checks file handling, argument parameters, file type,
and word/line length comparison.
Todo: Need to figure out what sort of memory the larger files might need.
*/
void close(FILE *fp1, FILE *fp2)
{
fclose(fp1);
fclose(fp2);
}
int wordwrap(FILE *ifp, FILE *ofp, char *wl)
{
char buf[MAX];
int c, i, j, space, count, length;
length = atoi(wl);
for (j = 0; j < MAX && ((c=getc(ifp)) != EOF); ++j)
buf[j] = c;
for (i = 0; i < j; ++i)
{
if ((int)buf < 0)
return 1;
if (buf == '\n')
count = space = 0;
if (buf == '\t')
count = count + 8;
if (buf == ' ')
space = i;
++count;
if ((count == length + 1) && (space == 0))
return 2;
else if ((count == length) && (space != 0))
{
buf[space] = '\n';
count = i - space;
}
}
for (i = 0; i < j; ++i)
putc(buf, ofp);
return 0;
}
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];
if (argc != 3)
{
printf("Usage: %s: filename, wrap length\n", prog);
return EXIT_FAILURE;
}
if (strlen(argv[1]) > 32)
{
printf("Filename limited to 32 characters. Sorry...\n");
return EXIT_FAILURE;
}
strcpy(filename2, argv[1]);
strcat(filename2, ".wrap");
if (atoi(wl) > 80)
{
printf("Line length limit: 80. Better is < 75.\n");
return EXIT_FAILURE;
}
if (atoi(wl) < 0)
{
printf("Line length must be a positive number.\n");
return EXIT_FAILURE;
}
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;
}
switch(wordwrap(fp1, fp2, wl))
{
case 0:
{
printf("Wrapping %s at %s\n", filename1, wl);
printf("Output file adds .wrap to input filename.\n");
close(fp1, fp2);
if (ferror(fp2))
{
fprintf(stderr, "%s: error writing %s\n", prog, filename2);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
case 1:
{
printf("Not an ASCII file.\n");
close(fp1, fp2);
return EXIT_FAILURE;
}
case 2:
{
printf("Word length exceeds specified line length.\n");
close(fp1, fp2);
return EXIT_FAILURE;
}
default:
{
printf("Unexplained program failure.\n");
close(fp1, fp2);
return EXIT_FAILURE;
}
}
}
----------------------------------------------------
I went with switch/case to avoid having to rewind the file for each check.
Is there a better approach to this?
Thanks for reading and reviewing!
------------------------------------------------
#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.
Done: Checks file handling, argument parameters, file type,
and word/line length comparison.
Todo: Need to figure out what sort of memory the larger files might need.
*/
void close(FILE *fp1, FILE *fp2)
{
fclose(fp1);
fclose(fp2);
}
int wordwrap(FILE *ifp, FILE *ofp, char *wl)
{
char buf[MAX];
int c, i, j, space, count, length;
length = atoi(wl);
for (j = 0; j < MAX && ((c=getc(ifp)) != EOF); ++j)
buf[j] = c;
for (i = 0; i < j; ++i)
{
if ((int)buf < 0)
return 1;
if (buf == '\n')
count = space = 0;
if (buf == '\t')
count = count + 8;
if (buf == ' ')
space = i;
++count;
if ((count == length + 1) && (space == 0))
return 2;
else if ((count == length) && (space != 0))
{
buf[space] = '\n';
count = i - space;
}
}
for (i = 0; i < j; ++i)
putc(buf, ofp);
return 0;
}
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];
if (argc != 3)
{
printf("Usage: %s: filename, wrap length\n", prog);
return EXIT_FAILURE;
}
if (strlen(argv[1]) > 32)
{
printf("Filename limited to 32 characters. Sorry...\n");
return EXIT_FAILURE;
}
strcpy(filename2, argv[1]);
strcat(filename2, ".wrap");
if (atoi(wl) > 80)
{
printf("Line length limit: 80. Better is < 75.\n");
return EXIT_FAILURE;
}
if (atoi(wl) < 0)
{
printf("Line length must be a positive number.\n");
return EXIT_FAILURE;
}
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;
}
switch(wordwrap(fp1, fp2, wl))
{
case 0:
{
printf("Wrapping %s at %s\n", filename1, wl);
printf("Output file adds .wrap to input filename.\n");
close(fp1, fp2);
if (ferror(fp2))
{
fprintf(stderr, "%s: error writing %s\n", prog, filename2);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
case 1:
{
printf("Not an ASCII file.\n");
close(fp1, fp2);
return EXIT_FAILURE;
}
case 2:
{
printf("Word length exceeds specified line length.\n");
close(fp1, fp2);
return EXIT_FAILURE;
}
default:
{
printf("Unexplained program failure.\n");
close(fp1, fp2);
return EXIT_FAILURE;
}
}
}
----------------------------------------------------
I went with switch/case to avoid having to rewind the file for each check.
Is there a better approach to this?
Thanks for reading and reviewing!