views:

212

answers:

4

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;

}
+1  A: 

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.

RichieHindle
I have tried, but error again, Ihave posted those codes after correction above.
Programme Newbie
@Programme Newbie: You need to post the actual error.
RichieHindle
To RichieHindle: New codes posted above. But the "Debug Error!" alert is shown when I run it.
Programme Newbie
@Programme Newbie: I've edited my answer, but *please post the actual error!* I'm sure the error you're getting says more than just "Debug Error!"
RichieHindle
Yes, "Run-Time Check Failure#3 - The variable 'num' is used without being intialized." also said by the alert.
Programme Newbie
@Programme Newbie: Ah, well there you go. Replace `char num;` with `char num = 0;`
RichieHindle
A: 

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?

Gregory Pakosz
count the numbers of digits
Programme Newbie
A: 

Counting letters you get to the end of the file and then trying to read some more from an uninitialised file handle...

Totophil
A: 

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.

Oz
Thanks, it works, but this solution seems informal. :)
Programme Newbie
It is pretty normal to close and re-open a file whenever you want to go back to the start. The alternative is to use `fseek( fp, 0, 0 )` which sets the file pointer back to the beginning.
Oz