Member Avatar for cooldev

Hi ,

IM TRYING TO WRITE A TEXT FORMAT PROGRAM along the lines of the algorithm & pascal code found in similar named problem in "how to solve it by computer" R.G.Dromey.
What it does is take a "limit" from user and users input text...it outputs the re-formatted text as "limit" characters per line.

Im struggling with translating pascal's -
i. while not eof(input)
ii. if eoln(input)
iii. readln
iv. read(chr)

i am using C's -

i. while (chr!=EOF);
ii. if (getc(stdin)=='\n');
iii. ??
iv. chr=getc(stdin);

for the above ones respectively.

While i know this is not a pascal forum...i paste my c effort below and also 2 sample runs. Any help much appreciated.

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0

void textformat(int limit);


int main()
{

int limit;

printf("Enter the line length limit\n");
scanf("%d",&limit);

textformat(limit);


printf("\n\n");
system("pause");
return 0;

}



void textformat(int limit) 
{
int i;
int linecnt;
int wordcnt;
int eol;
char chr;
char space;
char word[30];
char eoln;


FILE * outputFilePtr; 

i=0;
wordcnt=0;
linecnt=0;
eol=FALSE;

chr=' ';
space=' ';
eoln='\n';

for(i=0;i<=29;i++)
{
	word[i]=space;
}



outputFilePtr = fopen("afile.txt","w");

printf("Enter the text in multiple lines terminated by ctrl-z on a separate line\n");

while (chr!=EOF)
  {
    chr=getc(stdin);
    wordcnt=wordcnt+1;
    word[wordcnt]=chr;
	if (chr==space)
    {
	 if (eol && (wordcnt==1))
	    {
		 fprintf(outputFilePtr,"\n");
		 linecnt=0;
	    }
	 
  	 linecnt=linecnt+wordcnt;
	 if (linecnt>limit)
		{
		 fprintf(outputFilePtr,"\n");
		 linecnt=wordcnt;
		}
	 for (i=0;i<=wordcnt;i++)
		{
		 fprintf(outputFilePtr,"%c",word[i]);
		}
	 wordcnt=0;
	 eol=FALSE;
	 if(getc(stdin)==eoln)
	    {
		 eol=TRUE;
		}
   }
 
 }
  
fprintf(outputFilePtr,"\n");
fclose(outputFilePtr);

printf("\n\n");
printf("The re-formatted text is in afile.txt");

}

Sample runs -

1.

Enter the line length limit
8
Enter the text in multiple lines terminated by ctrl-z on a separate line
wherever you go
^Z


The re-formatted text is in afile.txt

Press any key to continue . . .

o/p file -

wherever
ou
-------------

as seen - the lasst word is eaten up and also first character of second word ..thou "limit"-8 has been adhered to


2.

Enter the line length limit
40
Enter the text in multiple lines terminated by ctrl-z on a separate line
wherever you go
^Z


The re-formatted text is in afile.txt

Press any key to continue . . .

o/p file-


wherever ou
-----------------------

again the same issue.

You're sort of close. I'm not absolutely clear of your exact requirements (with regards to what to do with word boundaries).

Look at the following code. I have hopefully simplified things for you a bit. Bear in mind that this program just outputs exactly "limit" characters per line (except perhaps for the last line) and does nothing "fancy" with word boundaries. So words will "break" across lines if the line size has been reached and consecutive words will be "butted" together if a newline character has been read before line size characters have been output.

Use it as a working template if you wish and modify it to your needs.

#include <stdio.h>
#include <stdlib.h>

void format_text(int lineSz);

int main(void) {

    int lineSz;     // line size

    printf("Enter the line length limit: ");
    scanf("%d", &lineSz);
    while(getchar() != '\n') {;}

    format_text(lineSz);
    return 0;
}

void format_text(int lineSz) {

    int ch;             // character from stdin
    int nChars = 0;     // chars per line counter
    FILE *outfile;

    if ((outfile = fopen("afile.txt", "w")) == NULL) {
        printf("Error opening file.");
        exit(EXIT_FAILURE);
    }

    printf("Enter the text in multiple lines terminated by ctrl-z on a separate line\n");

    while ((ch = getc(stdin)) != EOF) {

        // ignore newline characters and get next character
        if (ch == '\n') {
            continue;
        }

        nChars++;

        // output character to file if line size has not been reached
        // otherwise end current line and output current character
        // on next line
        if (nChars <= lineSz) {
            putc(ch, outfile);
        } else {
            putc('\n', outfile);
            putc(ch, outfile);
            nChars = 1;
        }
    }

    putc('\n', outfile);
    fclose(outfile);
}

Once you get your program working with character based I/O functions, perhaps you can try and rewrite it to use string based I/O functions.

Good luck!

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.