0

Why does my while loop not print the print statements I want before terminating? None of my print statements print in the terminal when my program exits. I can't see any other places where the loop might exit.

    while (1) {
        int num_read;
        // add the fd value into the fd_set value
        FD_SET(sock_fd, &read_fds);
        FD_SET(STDIN_FILENO, &read_fds);

        // choose which fds to watch
        select(sock_fd + 1, &read_fds, '\0', '\0', '\0');

        if (FD_ISSET(STDIN_FILENO, &read_fds)) { //check whether the fd value is in the fd_set

            num_read = read(STDIN_FILENO, buf, BUF_SIZE);
            if (num_read == 0) {
                printf("Print statement before terminating");
                break;
            }
            buf[num_read] = '\0';         // Just because I'm paranoid

            int num_written = write(sock_fd, buf, num_read);
            if (num_written != num_read) {
                perror("client: write");
                printf("Print statement before terminating");
                close(sock_fd);
                printf("Print statement before terminating");
                exit(1);
            }
        }

        if (FD_ISSET(sock_fd, &read_fds)) { //the socket with the server    
            num_read = read(sock_fd, buf, BUF_SIZE);
            buf[num_read] = '\0';
            printf("%s", buf); 
        }
    }
    printf("Print statement before terminating");
    close(sock_fd);
    printf("Print statement before terminating");
    return 0;
}
grizzthedj
  • 7,131
  • 16
  • 42
  • 62
static
  • 19
  • 2
  • 2
    Please paste the full code to a [minimal complete verifiable example](https://stackoverflow.com/help/mcve), thanks – jcarpenter2 Apr 02 '18 at 03:57
  • You are printing when you fail writing socket,reading stdin and read something on socket, if non of these conditions meet nothing will be printed, besides that add \n to printf – Pras Apr 02 '18 at 04:03
  • `select(sock_fd + 1, &read_fds, '\0', '\0', '\0');` use NULL, not '\0' – wildplasser Apr 02 '18 at 11:59

3 Answers3

1

Printf() is a library function which uses library buffer. By default library uses lined buffer mechanism to print data on terminal. To make printf() to print messages immediately use "\n" in each string passed to printf(). When process terminates buffer is flushed that is why you got your prints.

Please read this. https://stackoverflow.com/a/36573578/5694959

Monk
  • 756
  • 5
  • 16
1

You can use what Monc suggested. i.e. adding '\n' in end of your statement. Alternatively you can use fflush(stdout). which also clears printf buffer and prints on stdout.

Devidas
  • 2,479
  • 9
  • 24
0

Monc and Devidas have provided clues for possible explanations, but unless you are programming to an ancient system, exit() should flush the pending output in stdout and any other streams.

A more likely explanation is you execute your program in an environment where stdout is not tied to the terminal or whatever serves to display output on your system. Some Windows based IDEs and some online systems have inadequate output handling and make it difficult to debug such problems.

Posting a complete program is necessary to let readers investigate if the problem has its source elsewhere in your program.

chqrlie
  • 131,814
  • 10
  • 121
  • 189