Hi ,

I have one buffer size is 50 bytes and reading 50 byte data from file

unsigned char bufffer[50];

read(fd,buffer,50); // Consider each byte numerical is value is zero.

printf("@@%s@@",buffer); // Here i will get @@@@ only.

But I want @@(50 zero)@@ as output(i have to use %c or %s only). If 25th byte numerical value is zero. It will print only 25 bytes only but i want print entire buffer if it has null terminal(ZERO) middle of the buffer also)

Any help !!

Recommended Answers

All 3 Replies

One way to do it is to print each byte one at a time in a loop. %s is for null terminated strings. What you apparently have is a binary file, not a text file. There is no standard C function that will print that entire buffer the way you want it.

char buffer[BUFF_SIZE];

printf("string [");

for (i=0; i<BUFF_SIZE; i++)
{
    if (buffer[i] == '\0')
        printf("0");

    else
        printf("%c",buffer[i]);
}

printf("]\n");

there are more clever ways to do it but that gives you an idea

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.