Joe said:
Douglas G wrote:
Snip All
Allow me please to re-state the problem. I think what you want to do
is to re-format a text file and place the '\n' in a more convenient
place. Right?
There is no need to do this line by line. There is no need for
strings of any sort. Consider a short routine I wrote yesterday.
I guess I should guess give the whole story. The problem is that input is
sent straight through the program with no alterations whatsoever.
The input routine collects it, and ends the input with \n and then \0 and
returns the length of the string.
The troublesome routine doesn't do any word wrapping at all, as I have
adjusted the size in the header file to the ridiculous in order to try and
see any effects.
The routine is assuming that the length could be anywhere up to the maximum
size of the input 1000 characters. Which means it would need word wrapping
more than once until it reaches the end.
My intented approach was to take a starting point plus the row length and
start checking backwards for the first available whitespace and then
replace it with a newline. Then change the starting point to the current
position and then iterate through the loop until the next starting point
plus the row length exceeded the length of the string.
However the output looks like the program never ran because it doesn't wrap
doesn't complain, no segment fault. I've tried is as a single file program
to make sure nothing was lost by splitting things up. No changes
whatsoever. I've added the -pedantic -Wall switches and made a few
changes. So here is the complete program warts and all. Other suggestions
are welcome, since I don't have any programmers that I know of and would
welcome anything that help to program with better habits etc.
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line size */
#define ROW_LENGTH 25 /* length before fold */
int getline(char input_line[], int length_of_input);
void putline(char buffer[], int line_length);
void fold (char s[], int len);
/* does a word wrap at designated spots using ROW_LENGTH. */
int main()
{
int i, len, start_point;
char line[MAXLINE];
len=start_point=i=0;
while ((len = getline( line, MAXLINE)) > 0) {
/* start of word wrap */
fold(line, len);
putline(line, len);
}
return 0;
}
/* does the wordwrapping using ROW_LENGTH as the start point
and starts back until it finds a white space and changes
it to a new line and iterates through this until it exceeds
the length passed to it */
void fold(char buffer[], int len)
{
int start_point,i;
start_point=i=0;
while (len > (start_point +ROW_LENGTH)) {
i=start_point+ROW_LENGTH;
while ( buffer
!=' ' || buffer!='\t')
start_point=--i;
buffer[++i]='\n';
}
start_point=i=0;
return;
}
/* putline: displays the line */
void putline(char buffer[], int lim)
{
int i;
for (i=0; i <lim; ++i)
putchar(buffer);
return;
}
/* getline: read a line into s return length */
int getline(char s[], int lim)
{
int c,i;
for (i=0; i <MAXLINE-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s=c;
if (c=='\n') {
s=c;
++i;
}
s='\0';
return i;
}