Thread: execution crash

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    13

    execution crash

    I am working on counting the no. of words from a input file.
    i think the code is alright, however when i click on run,,,
    it shows the below photo,,,
    what should i do?

    my code:
    Code:
    #include <stdio.h>
    main()
    {
     	  int i, cnt=0;
     	  char ch;
     	  FILE *fp;
     	  fp=fopen("InFile.txt","r");
     	  ch=fgetc(fp);
     	  while (ch!=EOF){
    	  		if(ch==' ')
    	  		cnt++;
    			}
    			printf("No of words = %d\n", ++cnt);
    			fclose(fp);
    			}

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    • Learn how to indent properly.
    • Change ch to be an int instead of a char. getchar() returns an int, and you need to use this for certain kinds of input.
    • Check if fp is NULL after calling fopen(). This type of scenario could cause your crash.
    • Change your loop logic to actually read more than one char. You'll be looping with the same char over and over again.
    • If you're really concerned about the exact number of words, per se, then you have to come up with a better algorithm.... ie. reading "Hi there.\nTesting... testing...." will produce only 3 words I believe according to your current algorithm. Depends upon what type of file you're expecting, and how complicated you want to make this.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    you are reading a character only once... after that no effect on ch
    so loop while runs infinitely........

    your corrected code would be:

    Code:
    #include <stdio.h>
    main()
    {
     	  int i, cnt=0;char ch;
     	  FILE *fp;
     	  fp=fopen("InFile.txt","r");
     	  while (  (ch=fgetc(fp)  ) !=  EOF  )
                    {
    	  		if(ch==' ' || ch=='\n' || ch=='\t')
    	  		cnt++;
    		}
    	   printf("No of words = &#37;d\n", ++cnt);
    	   fclose(fp);
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    it should be int main
    and need to check the return value of fopen before using it
    and - for empty file you still report 1 word found
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  2. Replies: 0
    Last Post: 10-06-2007, 01:25 PM
  3. Can not debug a crash
    By hannibar in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2007, 10:02 AM
  4. What is the best way to record a process execution time?
    By hanash in forum Linux Programming
    Replies: 7
    Last Post: 03-15-2006, 07:17 AM
  5. FYI: asctime(gmtime(&mytime)) = crash!
    By anonytmouse in forum C Programming
    Replies: 2
    Last Post: 09-29-2003, 02:24 AM