SteveL
SteveL

Reputation: 3389

Python program not receiving output of printf

For an assignment I must do a quoridor game, the game must communicate with a referee written in python so that two quoridor clients can play one against other, for some reason what ever I printf at the c program the python instance doesn't receive, or my c program doesn't receive anything from the python referee via scanf

The way I run the referee is like that

./referee.py --black ./quoridor  --white ./quoridor 

The first command that it sends to the ./quoridor is the command "name" and the quoridor must respond with its name, if I run the ./quoridor and type name it works just find,it responds with the name. But thats not the case for the referee.

char command[MAX_COM_SIZE];
scanf("%s",command);
if(strcmpIgnoreCase(command,"name")==0){
    printf("= Athinaios\n\n");
}

The professor have provided me with a binary of a quoridor that works just fine and from the terminal its behaves exactly the same way as mine

Upvotes: 2

Views: 837

Answers (2)

Bing Bang
Bing Bang

Reputation: 544

There's no reason to do buffered i/o in these kinds of cases. I'd use a write call.

Upvotes: 1

kfx
kfx

Reputation: 8537

The printf() call in your code correctly puts the formatted string to stdout. However, the problem is that C standard library file descriptors are buffered, so the output will not appear until either the buffer is full or is explicitly flushed.

To flush the output, use fflush:

printf("= Athinaios\n\n");
fflush(stdout);

Or alternatively, disable buffering at the start:

setbuf(stdout, NULL);

Upvotes: 4

Related Questions