ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
please let me know how to read an binary file line by line ,i dont have problem in reading by making use of buffer,but i need to read an .bin file line by line a sample code will help me a lot
Like jf.argentino already said: By definition there is no such thing as a line in binary files.
In a binary file a newline can occur everywhere.
But it must not define a newline, it could be as well just a digit having the same value like a newline.
That's why the two modes (binary/text) to read/write a file exist.
If want that weird thing anyway, you have to write your own routine to get newline delimited chunks of a binary file.
The newline thing can be dealt with; what can't be dealt with using C-strings is \0. You can certainly read up to the newline, but you need to know how long the input is in case there is a \0 in there. I generally consider "text" to be data with only non-\0 characters, and "binary" to be data with any character.
Kevin Barry
I generally consider "text" to be data with only non-\0 characters, and "binary" to be data with any character.
Me too. Fortunately, in C99 you have getline() and getdelim() that handle arbitrary-length lines or binary data, by way of returning the explicit number of bytes read. Thus, the data is not really C strings, but character arrays. (The functions will still add an extra NUL (\0) after the data, so you can treat the buffer as a normal C string if you wish.)
Still, what you said does apply: the string functions will only consider the data up to the first NUL (\0) byte. GNU C library does provide a few extra functions, such as memmem(), memchr() and memrchr(), to handle such data correctly.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.