As this is a very basic word wrap program, it has at least the following 3 limitations :-
- The file should have a terminating newline.
- The column number at which the lines are to be wrapped must be greater than or equal to the length of the longest word.
- Tabs (
\t
) should not have been used in the lines to be wrapped.
Also, the usage of argv[0]
makes this code non-portable.
Here is the code :-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char * modified_fgets(char *, const int, FILE *);
static void wrap_line(char *, const int);
int main(int argc, char ** argv)
{
if (argc != 2)
printf("Enter this: %s filename\n", argv[0]), exit(EXIT_FAILURE);
FILE * in;
if ((in = fopen(argv[1], "r")) == NULL)
fprintf(stderr, "Can't open %s\n", argv[1]), exit(EXIT_FAILURE);
FILE * out;
if ((out = fopen("OUT.txt", "w")) == NULL)
fprintf(stderr, "Can't create output file\n"), exit(EXIT_FAILURE);
char s[1024];
while (modified_fgets(s, 1024, in) != NULL)
wrap_line(s, 80), fprintf(out, "%s\n", s);
if ((fclose(in) != 0) || (fclose(out) != 0))
fprintf(stderr, "Error in closing files\n");
return EXIT_SUCCESS;
}
// This function reads a line that is terminated by '\n'.
static char * modified_fgets(char * s, const int n, FILE * fp)
{
char * returnValue;
returnValue = fgets(s, n, fp);
int i = 0;
if (returnValue != NULL)
{
while ((s[i] != '\n') && (s[i] != '\0'))
i++;
if (s[i] == '\n')
s[i] = '\0';
else // s[i] == '\0' (i.e. there are extra characters in the line).
while (getc(fp) != '\n')
continue;
}
return returnValue;
}
// This function wraps a single line, terminated by '\0' instead of '\n'.
// wrapColumn should be >= the length of the longest word.
// '\t' should not have been used in the line to be wrapped.
static void wrap_line(char * s, const int wrapColumn)
{
int lastWrap = 0;
for (int i = 0; i < (int) strlen(s); i++)
if (i - lastWrap == wrapColumn)
for (int k = i; k > 0; k--)
if (s[k] == ' ')
{
s[k] = '\n';
lastWrap = k+1;
break;
}
}
Here is a sample input file :-
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
Here is the desired output file :-
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem
Ipsum has been the industry's standard dummy text ever since the 1500s, when an
unknown printer took a galley of type and scrambled it to make a type specimen
book. It has survived not only five centuries, but also the leap into electronic
typesetting, remaining essentially unchanged. It was popularised in the 1960s
with the release of Letraset sheets containing Lorem Ipsum passages, and more
recently with desktop publishing software like Aldus PageMaker including
versions of Lorem Ipsum.
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
in a piece of classical Latin literature from 45 BC, making it over 2000 years
old. Richard McClintock, a Latin professor at Hampden-Sydney College in
Virginia, looked up one of the more obscure Latin words, consectetur, from a
Lorem Ipsum passage, and going through the cites of the word in classical
literature, discovered the undoubtable source. Lorem Ipsum comes from sections
1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and
Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of
ethics, very popular during the Renaissance. The first line of Lorem Ipsum,
"Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
It is a long established fact that a reader will be distracted by the readable
content of a page when looking at its layout. The point of using Lorem Ipsum is
that it has a more-or-less normal distribution of letters, as opposed to using
'Content here, content here', making it look like readable English. Many desktop
publishing packages and web page editors now use Lorem Ipsum as their default
model text, and a search for 'lorem ipsum' will uncover many web sites still in
their infancy. Various versions have evolved over the years, sometimes by
accident, sometimes on purpose (injected humour and the like).
Edit :-
I guess the first limitation is easy to fix.
Using while (((c = getc(fp)) != '\n') && (c != EOF))
instead of while (getc(fp) != '\n')
in the modified_fgets()
function does the trick, where c is declared as an integer beforehand.
while (getc(fp) != '\n')
is a potential infinite loop shouldget()
return aEOF
due to a rare input error (even if file has a final'\n'
). \$\endgroup\$