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.

Recommended Answers

All 3 Replies

As usual, "doesn't seem to work" is not a helpful description of the problem. My wild guess is that you do not account for the newline character (which does appear in buff), and becomes an inherent part of certain words.

Deeply sorry Nezachem, let me explain in more detail. I have figured out a couple things in the past 30 minutes and maybe you could help.


NEW CURRENT CODE

#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," \n");
			while(word != '\0')
			{
				wordlen = strlen(word);
				if((wordlen + strlen(outline) < linelim ))
				{
					strcat(outline,word);
					strcat(outline," ");
				}
				else
				{
					fprintf(out,"%s\n",outline);
					memset(outline, '\0', sizeof(outline));
					strcat(outline,word);
					strcat(outline," ");
				}
				word = strtok(NULL, " \n");
			}
	}
return(0);
}

Like i said before I would like to limit each line to 20 characters.

My output file is getting scarily close to what its suppose to be.

this is a test to 
look at every 
character in an 
array and justify

thing to note
1. the last line is cut off
2. not sure if i need to have < 20 or <= 20 for the limit.

I forgot to put \n as a delimitor in strtok(), and that helped. But for the last line, ehh im a little confused how to get that on there.

Thanks

1. the last line is cut off

What causes you to exit the loop? Have you already output all the data when it exits?

2. not sure if i need to have < 20 or <= 20 for the limit.

Then you'll have to make a decision... There's an easy way to find out :icon_wink:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.