I am writing a function to do "while" to count the numbers of alphabetic and digits in a text file. I would like to seperate it to 2 functions of 2 "while". But it error after I create the first function. What's wrong of it?
#include "stdafx.h"
#include "stdlib.h"
#include "ctype.h"
void countDig (FILE* input, char num, int numCount);
int _tmain(void)
{
FILE* input;
char num;
char ch;
int numCount = 0;
int chCount = 0;
input = fopen("123.txt", "r");
if (!input)
{
printf("No file \a\n");
exit (101);
}
while ((fscanf(input, " %c", &ch)) == 1)
{
printf(" %c\n", ch);
if (isalpha(ch))
chCount++;
}
countDig (input, num, numCount);
printf("ch count: %d", chCount);
return 0;
}
void countDig (FILE* input, char num, int numCount)
{
FILE* f;
char n;
int nc;
while ((fscanf(f, " %c", &n)) == 1)
{
printf(" %c\n", n);
if (isdigit(n))
nc++;
}
printf("number count: %d", nc);
return;
}
after correction:
#include "stdafx.h"
#include "stdlib.h"
#include "ctype.h"
void countDig (FILE* input, char num, int numCount);
int _tmain(void)
{
FILE* input;
char num;
char ch;
int numCount = 0;
int chCount = 0;
input = fopen("123.txt", "r");
if (!input)
{
printf("No file \a\n");
exit (101);
}
while ((fscanf(input, " %c", &ch)) == 1)
{
printf(" %c\n", ch);
if (isalpha(ch))
chCount++;
}
countDig (input, num, numCount);
printf("ch count: %d", chCount);
return 0;
}
void countDig (FILE* input, char num, int numCount)
{
char n;
int nc = 0;
while ((fscanf(input, " %c", &n)) == 1)
{
printf(" %c\n", n);
if (isdigit(n))
nc++;
}
printf("number count: %d", nc);
return;
}