Help with a character counting program!

This is a discussion on Help with a character counting program! within the C Programming forums, part of the General Programming Boards category; Hi all, i've been working on this problem all morning (i'm a complete newb to C and only did it ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    7

    Help with a character counting program!

    Hi all, i've been working on this problem all morning (i'm a complete newb to C and only did it as part of my engineering course)

    The question reads :
    "Write a complete program to count the number of characters in a text file. The program should open a pre-existant text file (checking if the open operation was succesful), count the number of characters in the file (use a variable called numChars) and close the file afterwards.

    Heres my attempt thus far

    Code:
    #include <stdio.h>
    
    int main () {
        
    
    int numChars;
    
    FILE *Textfile;
    
    Textfile = fopen("C://list.txt", "r");
    
    if (Textfile == NULL)
    
    {
                 printf ("File failed to open.\n");
    
    } else {
        
    
    while ( (Textfile = getchar() ) != EOF )
    
    {
          numChars++;
     
    }
    
    }
    fclose(Textfile);
    
    printf("The %d of characters in the text file is:", numChars);
    
    return 0 ;
    
    }
    It seems to be mostly fine but problems are arrising in the while loop section, i think its due to the fact im using the pointer Textfile but am not entirely sure there could be a lot more problems knowing me. I also think i have to declare or include the txt file would i be right in thinking that ? Thanks in advance.

    James

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You just need a single foreward slash in your file name. You're thinking of backslash, which must be escaped.
    getchar reads from standard in (stdin), not from a file. Try fgetc, and just compare what it returns directly with EOF.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    7
    Thanks for the advice, i'm not overly familier with fgetc, it seems to be similar to getchar except that it reads the next character from a file as opposed to from stdin.

    So would i be correct in assuming it is implemented in the same way as getchar?

    eg .

    Code:
    
    
    while ( (Textfile = fgetc() ) == EOF )
    {
          numChars++;
     }
    I'm extremly in expierienced in using any of these features so you'll have to excuse my ignorance towards them!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    'Textfile' is your file. So you wouldn't assign what fgetc reads to that. You'd be just checking what was returned to see if it was EOF or not. Something like:
    Code:
    while( fgetc( Textfile ) != EOF )
        numChars++;
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    Check the Usage of fgetc
    int fgetc( FILE *stream );

    You may wanted something like below

    Code:
    while ( ( something = fgetc(Textfile)...... ))

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    7
    Thanks a lot! The program appears to be working now, well i'm getting the "File failed to open message" due to the fact that the file doesnt actually exist on my pc. Thanks for your help!

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    12
    Since you probably do not need the file length (which can be obtained with other methods), but the count of alphabetic characters, then I think your loop should look like this:

    Code:
    int c;
    
    while( (c = fgetc(Textfile) != EOF)
    {
    	if(isalpha(c)) ++numChars;
    }
    I hope this helps.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    7
    Hi, i think the question is asking for the count of every character, spaces etc. included. Does your way count spaces too ? Also what does "isalpha" mean i am not familier with it! Thanks for your advice though!

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    12
    The isalpha(c) function checks if c is an alphabetic character like 'a', 'b', etc. The previous program counts such letters only, excluding spaces or digits. If you need the number of all of bytes, then use

    Code:
    while(fgetc(Textfile) != EOF) ++numChars;
    but it does not seem to be the quicker way in my opinion.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Um, Viorel... where on earth did you get the "alphabetics only" idea?

    There *are* faster ways to obtain file size, using stat() and similiar functions, but I'm guessing using those functions won't help our little friend pass his course!
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with number counting program
    By Turtal in forum C Programming
    Replies: 11
    Last Post: 04-25-2009, 03:40 PM
  2. need help with 'character counting from a .txt file'
    By master_vst in forum C Programming
    Replies: 5
    Last Post: 11-09-2008, 02:17 PM
  3. Replies: 2
    Last Post: 12-02-2007, 05:40 AM
  4. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  5. character occurrence program not working
    By Nutshell in forum C Programming
    Replies: 6
    Last Post: 01-21-2002, 10:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21