top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to count the number of bytes in a stream that contains nulls in C?

+3 votes
425 views

I want to count the number of bytes in a stream that contains nulls. I know I can't use strlen() for this, is there an alternative?

char *stream = "\x11\x12\x13\x00\x12\x13\x14\x15";
posted Oct 14, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+2 votes

1:
To include null in your count then probably you need to make your own function which works like strlen()
which count your null character also.
But you have provide some break point inside your function to terminate it.

or
2:
You can copy your stream to a file (write to a file).
Then process this file till the end of file.
Keep a count for every character read you can use <fgetc()>

answer Oct 14, 2014 by Arshad Khan
+1 vote

This is basically a binary string & NOT a character string. So I don't think you can use any of the string related functions with this kind of data & achieve a valid result.

This is why we actually use a NULL character in our character array to differentiate between valid/invalid characters (end of string).

If this was an array & NOT a pointer, you could probably use sizeof(array) & you would get the total size of array & NOT the number of bytes of actual data.

You can use some sort of packing mechanism(encoding) & avoid the use of NULL character in the actual data.

answer Oct 14, 2014 by Ankush Surelia
Similar Questions
+4 votes

Say you are given a 2X2 board then number of squares are 5. Can someone help with the logic and sample program?

+2 votes

Words may be separated by any amount of whitespace characters. There can be integers in a file, but the program should only count words which have at least one alphabetic character.

+2 votes

Count how many integers from 1 to N contains 0's as a digit? Please share the sample c program?

...