I am writing a function to do "while" , but why error?
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;
}
Your countDig
function takes a FILE*
but doesn't use it. Instead it uses an unintialised local FILE*
:
void countDig (FILE* input, char num, int numCount)
{
FILE* f;
char n;
int nc;
while ((fscanf(f, " %c", &n)) == 1) // f is uninitialised here
You probably meant something like this:
void countDig (FILE* input, char num, int numCount)
{
char n;
int nc;
while ((fscanf(input, " %c", &n)) == 1)
You're also using nc
without initialising it - C doesn't automatically set variables to zero; you have to do that yourself:
int nc = 0;
Edit after followup code: As far as I can see you have a couple of other uninitialised variables, but apart from that the code should work. The only other issue is that countDig()
may not find anything because the initial loop in _tmain()
has read all the way to the end of the file. countDig()
will continue to read where the main loop finished, which could be the end of the file.
In your countDig
function, your File* f;
variable is not initialized, neither is nc
that you're incrementing.
Finally in this funciton, what was the intent of the numCount
parameter?
Counting letters you get to the end of the file and then trying to read some more from an uninitialised file handle...
You need to close and re-open the FILE*
. After the first while loop the FILE*
is at the end of the file, so when you try to loop through it again in the function it wont work.
精彩评论