Thread: neverending loop

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    neverending loop

    Hey guys i have the following code it reads a text file and list numbers to each word line. example:

    1. abfdfd
    2. hello world
    3
    4 so on..

    but at the moment its only listing the numbers and in a neverending loop so i have no idea whats going on.



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv)
    {
    
        int line_no = 0;
        int input_char = 0;
        int nl = 0;
        FILE *fp;
    
        if(argc !=2) /* check for 2 arguments*/
        {
    
          fprintf(stderr, "invalid usage :%s\n", argv[0]);
          return 1; /* stop processing if failes */
    
        }
    
        if((fp = fopen(argv[1], "r")) == NULL) 
        /* opens file for reading and checks if it exists */
        {
         
         fprintf(stderr, "invalid usage: %s\n", argv[0]);
         return 1; /* stop processing if fails*/
    
        }
    
    
          while(input_char !=EOF)
     
              /* keep asking for input while not end of file*/
          {
              if ( nl )
              {
                 printf("\n%d ", line_no);
                 line_no++;
    	  }
    	  else
              { 
    	     putc(input_char, stdout);
    	     input_char = getc(fp);   
    	     nl = input_char == '\n';
              }
        }
           
        
    
        fclose(fp);
    
    
    
        return EXIT_SUCCESS;
    
    }

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Are you a goldfish?

    You already posted the same question earlier, I answered it:

    http://cboard.cprogramming.com/showthread.php?t=69830

    The solution, partially, is to set nl to 0 again after you print the \n for a newline.
    Last edited by cwr; 09-19-2005 at 07:15 AM.

  3. #3
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Ok yehhhh i did intialize it to zero but why does it still loop

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    No, you need to set it to 0 here:
    Code:
    if ( nl )
    {
        printf("\n%d ", line_no);
        line_no++;
        nl = 0;
    }
    Otherwise as soon as you hit the first newline, it will be permantly stuck in the "found a new line" state.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    You are doing that the hard way. use fgets() and you don't have to worry about EOF.
    Code:
    char line[255];
    int line_counter = 1;
    while(fgets(line,sizeof(line),fp) != 0)
    {
       printf("%d%s", line_counter++,line);
    }

  6. #6
    kiddo
    Join Date
    Sep 2005
    Posts
    8
    hi bazzano,

    here is the solution for ur ques. im new to this website and to file handling. i have solved it with watever i know.
    hope u like it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv)
    {
    
        int line_no = 1;
        char input_char;
        FILE *fp;
        clrscr();
        if(argc !=2) /* check for 2 arguments*/
        {
    
          fprintf(stderr, "invalid usage :%s\n", argv[0]);
         return 1; /* stop processing if failes */
    
        }
    
        if((fp = fopen(argv[1],"r")) == NULL)
        /* opens file for reading and checks if it exists */
        {
    
         fprintf(stderr, "invalid usage: %s\n", argv[0]);
         return 1; /* stop processing if fails*/
    
        }
         printf("\n%d. ", line_no);
         while(input_char!=EOF)
    	  /* keep asking for input while not end of file*/
          {
          input_char = getc(fp);
          if(input_char=='\n')
    	  {
    	  line_no++;
    	  printf("\n%d. ", line_no);
    	  }
    	     if(input_char!='\n')
    	     putc(input_char, stdout);
    
        }
           printf("\b\b\b\b  \b\b");
        fclose(fp);
       return EXIT_SUCCESS;
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by mitto123
    hi bazzano,

    here is the solution for ur ques. im new to this website and to file handling. i have solved it with watever i know.
    hope u like it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv)
    {
    
        int line_no = 1;
        char input_char;
    
    
         while(input_char!=EOF)
    	  /* keep asking for input while not end of file*/
    Read the EOF FAQ as to why this is wrong.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM