Exit(0) and Infinite Loop

This is a discussion on Exit(0) and Infinite Loop within the C Programming forums, part of the General Programming Boards category; Code: while (running) { fgets(buffer, 128, stdin); if (buffer[strlen(buffer) - 1] == '\n') { buffer[strlen(buffer) - 1] = '

Thread: Exit(0) and Infinite Loop

'; } ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    67

    Exit(0) and Infinite Loop

    Code:
    while (running) {
    		fgets(buffer, 128, stdin);
    		if (buffer[strlen(buffer) - 1] == '\n') { buffer[strlen(buffer) - 1] = '\0'; }
    		for (i = 0; i < bots[0].count; i++) {
    			sprintf(sendbuf, "%s\r\n", buffer);
    			if (bots[i].connected) { senddata(i, sendbuf); }
    		}
    	}
    Code:
    void cmd_quit(void) {
    
    	int i = 0;
    	
    	running = 0;
    	for (i = 0; i < bots[0].count; i++) {
    		if (bots[i].s > 0) { destroysocket(i); }
    	}
    	WSACleanup();
    	free(bots);
    	
    	exit(0);
    
    	return;
    }
    Console window does not close. How can I overcome this?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I see nothing in your "while(running)" loop to break the loop. Also, it might be a good idea to check for EOF after the fgets().
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    I thought exit() would simply close the program. :|

    running = 0 is ment to break the loop. The problem is that fgets is blocking. I've tried select, which doesn't work on nonsockets in Windows. I've tried WaitForSingleObject. I've tried fputs("\n", stdin). I've tried closing stdin and checking for EOF. Nothing seems to get my program to actually break the loop and close.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, but who's calling cmd_quit? I don't see it in the loop anywhere.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    Quote Originally Posted by tabstop View Post
    Well, but who's calling cmd_quit? I don't see it in the loop anywhere.
    The thread receiving data from the server.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Unless senddata() is changing the status of running to zero, there is nothing else in your loop to cause running to become false and the loop to quit. Period.

    Now, if this is a multitasking application, then you failed to tell us that.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    Quote Originally Posted by Dino View Post
    Unless senddata() is changing the status of running to zero, there is nothing else in your loop to cause running to become false and the loop to quit. Period.

    Now, if this is a multitasking application, then you failed to tell us that.
    I already mentioned how cmd_quit is being called. The issue has nothing to do with my running variable, and everything to do with fgets.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    All the standard input methods block to wait for input. Although, if you really do close stdin, then fgets will stop. So if that didn't break out of the loop, nothing will. (Do you find that you have to type an extra line of input before it ends? Or does it just wait no matter what?)

  9. #9
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    Quote Originally Posted by tabstop View Post
    All the standard input methods block to wait for input. Although, if you really do close stdin, then fgets will stop. So if that didn't break out of the loop, nothing will. (Do you find that you have to type an extra line of input before it ends? Or does it just wait no matter what?)
    I have to hit enter before it breaks, which makes sense. I'm just trying to find a way around it.

  10. #10
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    Code:
    	if (fclose(stdin) == 0) {
    		printf("LOLSUCCESS\n");
    	} else {
    		printf("error: %d\n", errno);
    	}
    	printf("blah\n");
    Prints nothing.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Hm. Well, I wouldn't expect printing anything to stdin would work (if it's even set up to accept input, it would just go in the buffer I would expect). Does what you have not print anything, even after the last enter? (I would expect it to print something, eventually.) Which one does it print?

  12. #12
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    Quote Originally Posted by tabstop View Post
    Hm. Well, I wouldn't expect printing anything to stdin would work (if it's even set up to accept input, it would just go in the buffer I would expect). Does what you have not print anything, even after the last enter? (I would expect it to print something, eventually.) Which one does it print?
    I'm not printing to stdin. I'm closing stdin, and trying to print to stdout. Once fclose is called, nothing is printed. The line before the fclose call prints, but nothing after. Also, stdin is not actually being closed. When I hit enter, fgets completes and my bot sends the last thing I typed before closing.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    (I was referring to
    I've tried fputs("\n", stdin).
    above, above.)
    You could try writing to stdin, and then calling _flushall (I'm guessing, based on "WSA" above, that we're on a Win machine and are planning to stay that way.) I'm not convinced, but it might work.

    If you like to live life on the edge, you could look into _getchar_nolock which may allow you to close stdin on your input routine.

  14. #14
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    Quote Originally Posted by tabstop View Post
    (I was referring to

    above, above.)
    You could try writing to stdin, and then calling _flushall (I'm guessing, based on "WSA" above, that we're on a Win machine and are planning to stay that way.) I'm not convinced, but it might work.

    If you like to live life on the edge, you could look into _getchar_nolock which may allow you to close stdin on your input routine.
    I'm porting it to linux after completion. And no, I can't write to stdin. That was just a wild guess, before doing research.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, I still don't think there's anything portable you can do. According to my man pages here, you could turn _getchar_nolock into getchar_unlocked in POSIX, if that works for you in the first place. I've never used either, so I don't know what issues you'll run into.

Page 1 of 2 12 LastLast

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21