Im trying to read off an input text file that reads ie. "Three happy dogs eat." My goal is to count the total characters and print ONLY that to the screen AND create an output txt file to read like this:
Three 5
happy 5
dogs 4
eat 3
I have counted the total characters and created the output file but haven't counted the individual characters of each word.
so my current output file is:
Three
happy
dogs
eat
How can i count and put the number of characters for each word?
Code:
/* Include Files */
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#pragma warning(disable:4996)
/*that will stop the warning on scanf being deprecated */
/*fx prototypes*/
int countAll ();
int getstuff ();
int main( void )
{
getstuff ();
countAll();
return 0;
}
/***** fxs *****/
int getstuff( void )
{
FILE *infile;
FILE *outfile;
int iochar;
int num = 0;
outfile = fopen("outtext.txt", "w");
if (( infile = fopen( "cscdata.txt", "r" )) == NULL ) {
printf( "text couldn't be opened\n" );
exit( 1 );
}
while ( !feof( infile )) {
while ( isalpha( iochar = getc( infile )))
putc( iochar, outfile );
putc( '\n', outfile );
ungetc( iochar, infile );
while ( isdigit( iochar = getc( infile )))
putc( iochar, outfile );
putc( '\n', outfile );
ungetc( iochar, infile );
while ( !isalpha( iochar = getc( infile )) && ( !isdigit( iochar )) && ( !feof( infile )))
;
ungetc( iochar, infile );
}
fclose( infile );
fclose (outfile);
return 0;
}
int countAll(void)
{
FILE *infile;
int c = 0;
int count = 0;
infile = fopen("cscdata.txt", "r");
while (c != EOF)
{
count++;
c = getc(infile);
}
printf("number of all characters = %d\n", count);
fclose (infile);
return 0;
}
I was thinking of using strlen()? like
Code:
while ( isalpha( iochar = getc( infile )))
putc( iochar, outfile );
num = strlen( iochar);
putc (num, outfile);
putc( '\n', outfile );
ungetc( iochar, infile );
can anyone point me in the correct way?