This may be a rather simple thing to answer, but I am no programming expert.
Let me explain my problem in detail:
for this homework assignment I need to open up a file with simple text in it, format it so that each line is reduced to a certain number of characters, and then put the formatted lines in a new file.
I came up with a simple text file to illustrate what i'm talking about
What it looks like
this is a test to look at
every character in an
array and justify
it by 20 characters
What it should look like:
this is a test to
look at every
character in an array
and justify it by 20
characters
*note, if your in the middle of a word when you reach the limit for a line, push it to the next line.
Using the algorithm on the assignment sheet i was able to come up with this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char buff[100];
char *word;
int linelim = 20;
int wordlen = 0;
char outline[50];
FILE *in;
FILE *out;
in = fopen("test.txt", "r");
out = fopen("out.txt", "w");
memset(outline, '\0', sizeof(outline));
while(fgets(buff,sizeof(buff),in) != NULL)
{
word = strtok(buff," ");
while(word != NULL)
{
wordlen = strlen(word);
if((wordlen + strlen(outline) <= linelim ))
{
strcat(outline,word);
strcat(outline," ");
}
else
{
fputs(outline, out);
memset(outline, '\0', sizeof(outline));
strcat(outline,word);
}
word = strtok(NULL, " ");
}
}
return(0);
}
But it doesn't seem to work, and when i step through the program, I think it has something to do with my use of fputs() but it's not very clear.
Does anybody see anything simple that I'm not seeing or can give me an insight. Like I said I'm far from a good programmer.
Thanks.