How do I print this?

  • Thread starter Olaf \El Blanco\
  • Start date
O

Olaf \El Blanco\

typedef struct { unsigned char data[MAX_DATA]; } packet;

typedef struct {
packet info;
int nro_reg;
} Buffer_Network;

void test(void)
{
FILE *filename;
Buffer_Network toRead;
filename = fopen("filename", "r");
while (fread(&toRead, sizeof(Buffer_Network), 1, filename)==1)
xxx(&toRead.info);
fclose(buffer_red);
}


void xxx(packet *p)
{
int i;
I need to print the PACKET p,
¿¿ for (i=0; i<=MAX_DATA; i++)
printf ("%c", *(p+i)); ??
¿¿ printf ("%s", p);

}
 
V

Vladimir Oka

Olaf said:
typedef struct { unsigned char data[MAX_DATA]; } packet;

typedef struct {
packet info;
int nro_reg;
} Buffer_Network;

void test(void)
{
FILE *filename;
Buffer_Network toRead;
filename = fopen("filename", "r");
while (fread(&toRead, sizeof(Buffer_Network), 1, filename)==1)
xxx(&toRead.info);
fclose(buffer_red);
}


void xxx(packet *p)
{
int i;
I need to print the PACKET p,
¿¿ for (i=0; i<=MAX_DATA; i++)
printf ("%c", *(p+i)); ??
¿¿ printf ("%s", p);

}

Wouldn't it be more natural to have:

void xxx(Buffer_Network *p);

and within it output:

printf("%c",p->info.data);

or

printf("%s",p->info.data);

provided it's zero terminated?
 
T

thampan

depends on what your packet contains ..

if your packet contains an ascii human readable string (null
terminated), use %s,
if your packet is a byte stream , use %c
 
B

Barry Schwarz

typedef struct { unsigned char data[MAX_DATA]; } packet;

typedef struct {
packet info;
int nro_reg;
} Buffer_Network;

void test(void)
{
FILE *filename;
Buffer_Network toRead;
filename = fopen("filename", "r");
while (fread(&toRead, sizeof(Buffer_Network), 1, filename)==1)
xxx(&toRead.info);
fclose(buffer_red);
}

Please include your question in the text of your message. Some
newsreaders do not provide visibility to the subject.
"How do I print this?"
void xxx(packet *p)
{
int i;
I need to print the PACKET p,
¿¿ for (i=0; i<=MAX_DATA; i++)
printf ("%c", *(p+i)); ??

This will print all MAX_DATA characters, without regard to how many
are actually used if the array contains a string. Is that what you
want?

printf is a pretty expensive function if you aren't taking advantage
of it's many features. putc might be more efficient. As a matter of
style, I find p easier on the eyes and more intuitive than *(p+i).
¿¿ printf ("%s", p);

This will only work if the array actually contains a string.
Similarly, fputs might be more efficient.


Remove del for email
 
D

Dave Thompson

typedef struct { unsigned char data[MAX_DATA]; } packet;

typedef struct {
packet info;
int nro_reg;
} Buffer_Network;
Buffer_Network toRead;
xxx(&toRead.info);

Note: in the OP code there is no declaration of xxx() before this
call, and the definition comes after, so in C90 it is implicitly and
wrongly declared as returning int (better compilers should give at
least a warning) and in C99 it is illegal.
Please include your question in the text of your message. Some
newsreaders do not provide visibility to the subject.
"How do I print this?"
As written this tries to access subsequent _packets_ (which don't
exist) and causes Undefined Behavior by (grossly) mismatching the
actual argument type with the format specifier for it.

Should be p->data (clearest) or equivalently
*(p->data + i) or *((*p).data + i) etc.
This will print all MAX_DATA characters, without regard to how many
are actually used if the array contains a string. Is that what you
want?
Actually with the fix above it will print MAX_DATA + 1, going off the
end of the array and possibly invoking Undefined Behavior, although
arguably not in this case since it's in a struct followed by something
else that can be accessed as unsigned char(s).
printf is a pretty expensive function if you aren't taking advantage
of it's many features. putc might be more efficient. As a matter of
style, I find p easier on the eyes and more intuitive than *(p+i).

Concur, see above.

To output all the characters (bytes) in the buffer as is, either of
fwrite (p->data, sizeof p->data, 1, stdout)
fwrite (p->data, 1, sizeof p->data, stdout)
or obvious/trivial variants thereof.
This will only work if the array actually contains a string.
Similarly, fputs might be more efficient.
Concur.


- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top