LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-05-2012, 04:33 AM   #1
shamjs
Member
 
Registered: Sep 2011
Posts: 93

Rep: Reputation: Disabled
Read binary file line by line


Hi all,

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








waiting for your inputs.........
 
Old 04-05-2012, 06:23 AM   #2
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
How do you define a line? If it's some data ended by character "\n" (or "\r" or both), there's no line in a binary file!
 
1 members found this post helpful.
Old 04-05-2012, 07:25 AM   #3
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
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.

What are you really trying to do?
 
Old 04-05-2012, 08:12 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 04-06-2012, 02:02 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by ta0kira View Post
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash : read every line from text file starting at given line number quadmore Programming 4 02-20-2009 12:29 PM
help with c program to read each line from text file, split line , process and output gkoumantaris Programming 12 07-01-2008 12:38 PM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration