Thread: program hangs during runtime

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    76

    program hangs during runtime

    At runtime, after I type in the name of my executible, it newlines and hangs. I get no error at compile time. I'll post the file I'm reading from if anyone wants it. Is there a problem with the code?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main () {
         
         char buf[11];            /* file buffer area */
         int lines, chars, x;    /* count of lines and characters */
         FILE *in;                /* input file stream pointer */
    
         in = fopen("d5.dat", "r");
        
         if (in == NULL) {
              perror("fopen:     d5.dat");
              exit(1);
         }
               
         fgets(buf, 11, in);
         while(!feof (in)) {
             
              for(x = 0; buf[x] != '\0'; x++)
                   if(buf[x] == '\n')
                        lines++;
                   if(isalpha(buf[x]))
                        chars++;
         }              
         
         fclose(in);
         printf("\nLines:%d\nCharacters:%d\n\n", lines, chars); 
         return 0;     
    }

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    My guess without seeing your file is that it is longer than 11 characters, hence this:
    Code:
    while(!feof (in)) {
    Never breaks you out of your loop.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    The contents of d5.dat:
    Code:
    
    
    Isn't counting lines fun....
    
    You should
    see some interesting results.
    
    
    Be careful to watch for newlines...

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Ok I see what your saying.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Ok I figured out what I needed to do, but I get a coredump when I run in UNIX. In Windows, under bloodshed, the executable runs fine. I think the character count is off, but that shouldn't cause a coredump. This is what I got:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main () {
         
         char buf[11];	        /* file buffer area */
    	 int lines, chars, x;	/* count of lines and characters */
    	 FILE *in;		        /* input file stream pointer */
    
         lines = 0;
         chars = 0;
         in = fopen("d5.dat", "r");
        
         if (in == NULL) {
              perror("fopen:     d5.dat");
              exit(1);
         }
               
         
         while(!feof (in)) {
              
              fgets(buf, 11, in);
              
              for(x = 0; buf[x] != '\0'; x++)
                   if(buf[x] == '\n')
                        lines++;
              
              for(x = 0; buf[x] != EOF; x++)
    		       if(isalpha(buf[x]))
    			        chars++;
         }              
         
         fclose(in);
         printf("\nLines:%d\nCharacters:%d\n\n", lines, chars);
         printf ("Press ENTER to continue.\n");
         getchar ();
         return 0;     
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while(!feof (in))
    You don't listen - read the FAQ!!!

    > for(x = 0; buf[x] != '\0'; x++)
    > for(x = 0; buf[x] != EOF; x++)
    Spot the difference yet?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Ok made a booboo with EOF. It can't look for EOF in data. Thats big faq to memorize everything, but thanks for showing me. The only prolem I have left is when I do:
    Code:
    [academ] $ wc d5.dat    
          13      16     115 d5.dat
    and then a run my executable, I get 13 lines, but only 102 characters. Should I be using something else instead of isprint?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main () {
         
         char buf[11];            /* file buffer area */
        int lines, chars, x;    /* count of lines and characters */
        FILE *in;                /* input file stream pointer */
    
         lines = 0;
         chars = 0;
         
         if ((in = fopen("d5.dat", "r")) == NULL) {
              perror("fopen:     d5.dat");
              exit(1);
         }     
         
         while(fgets(buf, 11, in) != NULL) {
              
              /* fgets(buf, 11, in); */
                        
              for(x = 0; buf[x] != '\0'; x++)
                   if(buf[x] == '\n')
                        lines++;
              
              for(x = 0; buf[x] != '\0'; x++)
                   if(isprint(buf[x]))
                        chars++;
         }              
         
         fclose(in);
         printf("\nLines:%d\nCharacters:%d\n\n", lines, chars);
         printf ("Press ENTER to continue.\n");
         getchar ();
         return 0;     
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    wc counts all characters - printable or not.

    Perhaps you just need to count the length of each buffer - say using strlen
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Cool thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-27-2006, 08:35 AM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Simple copy program hangs?
    By curlious in forum Linux Programming
    Replies: 3
    Last Post: 07-24-2004, 05:00 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM