Good day!
I am trying to build a C program for a character count, but it does not work for some reason. Can someone please tell me why?
character count:
Code:
#include <stdio.h> /* Standard IN/OUT library. */
#include <stdint.h> /* Standard extended....... library. */
/* #include <io.h> */ /* This library is not present in this compiler. */
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <math.h>
char *CharacterCount (char *InputFilename, char *ReturnedString)
{
// Local variables.
int i, n, b, t, iAll;
char sAll[5], sBlanks[5], sTabs[5], sNewLines[5];
//char sC[5];
char c;
//char sStr[100];
FILE *ifp;
char *InMode = "r";
ifp = fopen(InputFilename, InMode);
if (ifp == NULL)
{
return "-1";
/* MS DOS. */
//fprintf(stderr, "Can't open input file %s!\n", InputFilename);
exit(-1); /* Maybe should be "1". */
}
for (i=0, n=0, b=0, t=0, iAll=0; ; i++) /* The use of "while" will lead to wrong reading of the characters(44141242145), */
{ /* probably counts the addresses not the characters. */
c = fgetc(ifp); /* Automatic incrementation, after every read the counter is set to the next element. */
if (c == EOF)
break;
if (c == '\n')
n++;
if (c == ' ')
b++;
if (c == '\t')
t++;
iAll++;
}
fclose(ifp); /* All files must be closed. */
/* Convert int iAll to string sAll. */
itoa (i, sAll, 10);
/* Convert int c to string sC. */
//itoa(*c, sC, 10);
/* Convert int Blanks to string sBlanks. */
itoa(b, sBlanks, 10);
/* Convert int Tabs to string sTabs. */
itoa(t, sTabs, 10);
/* Convert int NewLines to string sNewLines. */
itoa(n, sNewLines, 10);
/* Convert all the text and char variables into 1 string. */
sprintf(&ReturnedString [0], "All == %s,\nCharacters == %s,\nBlanks == %s,\nTabs == %s,\nNewlines == %s.", sAll, sBlanks, sTabs, sNewLines);
/* This is the only way that works, on the forums they say its not safe. */
return "0";
}