Counting Characters

This is a discussion on Counting Characters within the C Programming forums, part of the General Programming Boards category; How do I write a program to count the number of characters in an external file. I have most of ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    3

    Counting Characters

    How do I write a program to count the number of characters in an external file. I have most of it down , this is what I have so far:
    Code:
    {
    	FILE*  file;
    	char	fileName[MaxStringLength];
    	char	c;
    	int	  numChars;
    	
    	printf( "Enter the name of a file to show: " );
    	fgets( fileName, sizeof( fileName ), stdin );
    	if( fileName[strlen( fileName ) -1] == '\n' );
    		fileName[strlen( fileName ) -1] = '\0';
    	
    	file = fopen( filename, "r" );
    	
    	if( file == NULL )
    	{
    		printf( "Unable to open file: %s\n", fileName );
    		return( 1 );
    	}
    	
    	numChars = 0;
    	while( (c = fgetc( file )) != EOF ) //not sure if this is right.
    	{
    	
    	}	
    	
    	return( 0 );
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    why not use strchr to find a char in a string. strchr('\n')

    and to count the number of char's in the string

    have a count variables with in the while loop and increment that every time u a read char from the file, and when reached the EOF file print the count

    ssharish2005

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by jrdoran
    How do I write a program to count the number of characters in an external file. I have most of it down , this is what I have so far:
    Good first poke.
    Code:
     char	c;
    	/* ... */
    	while( (c = fgetc( file )) != EOF ) //not sure if this is right.
    Since fgetc returns an int, c should be an int.
    Code:
    	printf( "Enter the name of a file to show: " );
    	fgets( fileName, sizeof( fileName ), stdin );
    I like to add an fflush(stdout); after the prompt to make sure I can see it.
    Code:
    	if( fileName[strlen( fileName ) -1] == '\n' );
    		fileName[strlen( fileName ) -1] = '\0';
    I believe what ssharish2005 was saying is what the FAQ shows.
    Code:
    	file = fopen( filename, "r" );
    Depending on your needs, you may want to open the file in binary mode ("rb").
    Code:
    	if( file == NULL )
    	{
    		printf( "Unable to open file: %s\n", fileName );
    		return( 1 );
    	}
    EXIT_FAILURE, EXIT_SUCCESS, or 0 would be standard.
    Code:
    	while( (c = fgetc( file )) != EOF ) //not sure if this is right.
    	{
    		/* probably a good place to increment your counter */
    	}
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting the characters from each word from a text file
    By flipguy_ph in forum C Programming
    Replies: 6
    Last Post: 04-27-2009, 06:56 PM
  2. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  3. Help with hw problem: counting characters in a string
    By pinkfloyd4ever in forum C++ Programming
    Replies: 11
    Last Post: 11-04-2007, 11:18 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  5. Counting characters
    By tay_highfield in forum C Programming
    Replies: 3
    Last Post: 01-29-2003, 12:54 PM

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