Using fread if the buffer size is not known

Hi...

I am trying to read a binary data that have different types of messages of different lengths. I am using fread() but this functions needs the size and count to read the buffer from the file. I think this may cause that the buffer overlaps other messages.

Is there an alternative to read from a binary file without specifying the length?

fread will read data and place it into arrays that you allocate. The size and nitems tell fread how much space you allocated for it to work with. Without them, the data might overflow your buffer and clobber other data. This is the "buffer overflow" bug that hackers love to see in a program.

Yes, you can use gets(). There is no way to specify a length to gets. If it overflows your buffer, tough. But writing gets() the way it is was a big mistake. And if you use it, you too are making a big mistake.