Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 10-05-2011, 01:10 AM   #1 (permalink)
Alpha Geek
 
Join Date: Jan 2007
Location: In your hearts
Posts: 828
Default count number of whitespaces in C


Hello everybody, I was doing a simple program to count number of whitespaces in a text file but I am stuck at it.
Here is the code

Code:
void main()
{
    FILE *from;
    int ch,count;
    int c;
    from = fopen("newfile","r");
    
    while ((c = fgetc(from)) != EOF)
    { 
        if (c == '\n' || c == '\t' || c == ' ')
        {
            count++;
           
            }
        }
    printf("%d",count);
    fclose(from);
    }
and in the file it is written

HELLO EVERY ONE \n

and it prints count in millions!!
whats the problem here please tell me!!
abhijangda is online now   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 10-05-2011, 02:05 AM   #2 (permalink)
Wise Old Owl
 
pulkitpopli2004's Avatar
 
Join Date: Jul 2010
Location: D!ll!
Posts: 1,131
Default Re: count number of whitespaces in C

initialise the count to zero
__________________
I'd explain it to you, but your brain would explode... Trust me :hot:
i5 760, Asus P7P55D-EVO, Zotac GTX 460, VX550, Win7 64bit,4GB RAM @1600MHz, WD 500GB Caviar Black
pulkitpopli2004 is online now   Reply With Quote
Old 10-05-2011, 09:36 AM   #3 (permalink)
Alpha Geek
 
Join Date: Jan 2007
Location: In your hearts
Posts: 828
Default Re: count number of whitespaces in C

ooppsss!!! Didn't remember to that
thx!!!
abhijangda is online now   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:27 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2