Thread: infinite loop

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    infinite loop

    I've written a code which is supposed to take the input from the user, save it in a file called "name" and then print it in the reverse order. But its getting stuck in an infinite loop, printing only the last character typed. The logic of the program is that, the getc(fp) in putchar(getc(fp)) will return a character and advance the file pointer by 1. The putchar will print the character and the condition in the while loop will bring the cursor back 2 places so that the next charcter can be printed...

    Code:
    #include<stdio.h>
    #include<string.h>
     
    int main()
    {
    	FILE *fp;int n;
    	char c;
    
    	fp=fopen("name","w");
    
    	if(fp==NULL)
    		printf("\nUnable to open\n");
    
    	while((c=getchar())!=EOF)
    		putc(c,fp);
    
    	printf("\nNo. of characters = %ld\n",ftell(fp));
    	fclose(fp);
    
    	fp=fopen("name","r");
    	n=0l;
    
    while(feof(fp)==0)
    {
    	fseek(fp,n,0);
    	printf("Position of %c is %ld\n",getc(fp),ftell(fp));
    	n=n+5l;
    }
    
    putchar('\n');
    
    n=ftell(fp);
    fseek(fp,n-1,0);
    
    do
    {		putchar(getc(fp));
    }while(!fseek(fp,-2L,1));
    
    fclose(fp);
    
    }
    Last edited by juice; 12-18-2011 at 04:19 AM.

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Also I need to know, why does the last line that the first while loop prints is

    Code:
    "Position of   is 12"...
    12 on my machine, and whatever you input on yours.

    I am only interested in the blank space which always gets printed in the last line when n has exceeded the bounds.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Also, this program when executed in MSVC 2010 with input "asdfghjklpo^z^z", gets stuck in an infinite loop, and with pelles C, it prints out all (except the last) characters in the reverse order...

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    GOD!! This is really freakin' me out. Plz help.
    There's some substitution character which is getting appended at the end of the string in MSVC.
    I wonder what that is.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It might help if you used the symbolic constants like SEEK_SET (if for no other reason, readability) rather than the numeric constants 0, 1 etc.
    fseek

    Add some debug code to the loop in question, to say print the values retuned by fseek, ftell, feof, and ferror.

    Use a debugger to step through the loop, rather than just letting it run forever.
    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.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    after hours of debugging all I got was that there is some substitution character (hex code 1A) which gets appended at the end of the string in MSVC and its behaviour is so abstruse I can't even explain.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > putchar(getc(fp));
    Split it into two statements.

    Code:
    int ch = getc(fp);
    if ( ch != EOF ) {
      // print it
    } else {
      // message
    }
    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.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juice View Post
    after hours of debugging all I got was that there is some substitution character (hex code 1A) which gets appended at the end of the string in MSVC and its behaviour is so abstruse I can't even explain.
    Look it up on an ascii chart ... I think you'll find it's the SUB character #26 which corresponds to CTRL-Z
    Which windows uses as an end of file marker and you typed on your keyboard.

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    does fclose automatically put an EOF marker at the end of file?

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    There is no EOF marker in the file. It's a product of the C library. It means the file position indicator is at the end of the file.

    I think at issue here is you that you are mis-using fseek. It is not for trying to find or trigger EOF.

    Quote Originally Posted by C99 draft 7.13.3 Files
    199. Setting the file position indicator to end-of-file, as
    with fseek(file, 0, SEEK_END), has undefined behavior
    for a binary stream
    However, your file is actually opened as a text stream, since there is no "b" in the fopen mode. So your setting the file position to EOF with fseek, which you do in both loops, is perhaps okay, but:

    Quote Originally Posted by C99 draft 7.13.9.2 The fseek function
    [#4] For a text stream, either offset shall be zero, or
    offset shall be a value returned by an earlier successful
    call to the ftell function on a stream associated with the
    same file and whence shall be SEEK_SET.

    [#6] The fseek function returns nonzero only for a request
    that cannot be satisfied.
    But you do not use a valid value. You use a value greater than the length of the file, which could not possibly be one returned by ftell() unless you used fseek first. So you do (perhaps) get EOF set, but you are also invoking undefined behavior. Which does not mean the request was not satisfied, it just means the satisfied request could lead to anything.

    The same is probably true when you use fseek to go backward beyond the beginning of the file, since again, that is outside the range of values ftell() could return if you weren't using fseek first.

    The moral is: do not use an offset other than 0 unless you know it is a valid location inside the file. Not 1-5 bytes "outside" of it. If ftell() returns 0, don't call fseek SEEK_CUR -1, etc.
    Last edited by MK27; 12-18-2011 at 12:28 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-14-2011, 11:33 PM
  2. Infinite loop!!!
    By Bizmark in forum C Programming
    Replies: 3
    Last Post: 02-21-2009, 12:09 PM
  3. cin infinite loop
    By tezcatlipooca in forum C++ Programming
    Replies: 5
    Last Post: 01-16-2007, 12:20 PM
  4. infinite while loop
    By beon in forum C Programming
    Replies: 15
    Last Post: 12-06-2006, 01:17 PM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM