Rory Flast Rory Flast - 1 month ago 9
C Question

Multi Client Chat Program: send messages to all Clients

How do I print

byte_length
bytes starting at pointer
address
into a log file.

I tried this but it didnt work because dereferencing the pointer is not a string literal.

FILE * userlog;
void debug_message(int flag, void *address, int byte_length){
for (i=0; i<byte_length; i++){
fprintf(userlog, *((char*)address+i)));
}


I also tried this, but it wouldn't compile:

FILE * userlog;
void debug_message(int flag, void *address, int byte_length){
for (i=0; i<byte_length; i++){
char character[1];
character[0]= *((char*)address+i));
fprintf(userlog, character);
}

LPs LPs
Answer

fprintf second parameter is the format specifier as you can see on The Man

If you really want to write a single char/byte %c

FILE * userlog;
void debug_message(int flag, void *address, int byte_length){
      for (i=0; i<byte_length; i++){
         char character = *((char*)address+i));
         fprintf(userlog, "%c", character);
}