I have a weird issue with printing data out. I use printf to print a char* string and then after that print another one. However part of the first string doesn’t get printed and when I print the second string the missing part of the first one is prepended to that one. What is happening here?
I’m writting a simple libpcap implimentation. Here is a sample callback function that will produce the same results. I tried removing buffering and adding a putchar(‘\n’) after printing but it didn’t help.
void ParseData(u_char* useless, const struct pcap_pkthdr* pkthdr, const u_char* packet){
int packetLen, i;
packetLen = pkthdr->len;
for (i = 0; i < packetLen; i++){
putchar(packet[i]);
}
}
stdiobuffers characters. Unless you tell it otherwise, usually it will only actually issue a write when it sees a newline character. If you want a different behavior, you can remedy it with some of these:After your first
printf, callfflush(stdout);to flush the buffer.Alternatively, call
setbuf(stdout, NULL);to disable buffering. Do this before you do anyprintfs.Bypass
stdioby coding to platform specific APIs likewrite(POSIX) orWriteFile(Windows). Usually I would recommend against this, especially for something likestdout..