0

I was writing a chat server program as part of my network lab when I observed a very strange behavior:

        printf("Recieved login info.\n"); //works
        printf("Username: %s\n", ui->username); //works
        printf("Passward: %s\n", ui->password); //works

        //TO_AUTHENTICATE, if failure, send appropriate reply
        printf("makeitwork"); //does not work
        while(ui != NULL)
        {
                printf("here2");//does not work
                if(strcmp(ui->username, record.username) == 0 && strcmp(ui->password, record.password) == 0)
                {
                        strcpy(userRecords[sockfd].username, record.username);
                        userRecords[sockfd].status = ONLINE;

                        printf("Successfully authenticated %s.\n", userRecords[sockfd].username);

                //      sendOnlineUsers(sockfd, registeredUsers);

                        return SUCCESS;
                }
                ui = ui->next;
        }

In the above code segment, the first, second and third printf statements works, but from the 4th onwards, it does not work.

But if I comment out the if statement printf("here2");, everything works normally.

I used gdb to execute the program line by line:

(gdb) 
Recieved login info.
94      printf("Username: %s\n", ui->username);
(gdb) 
Username: user-1
95      printf("Passward: %s\n", ui->password);
(gdb) 
Passward: pass
98      printf("makeitwork");
(gdb) 
99      while(ui != NULL)
(gdb) 
101         printf("here2");
(gdb) 
102         if(strcmp(ui->username, record.username) == 0i)//&& strcmp(ui->password, record.password) == 0)

The printf is executing, but there is no output on the terminal.

What is happening?

daltonfury42
  • 1,675
  • 2
  • 17
  • 32

1 Answers1

1

You need to flush the buffer

Add

fflush(stdout);

after the printf where you do not have \n at the end

Ed Heal
  • 55,822
  • 16
  • 77
  • 115
  • You were right. I put a `/n` and now it is working. Can you explain what is happening, and I'll accept your answer. – daltonfury42 Feb 13 '17 at 18:18
  • `Printf` puts stuff in a buffer. `\n` flushes the buffer and sends it to the terminal. Either send the new line or flush the buffer as described above – Ed Heal Feb 13 '17 at 18:20