Computing.Net > Forums > Programming > C : read/write bit by bit a binary

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C : read/write bit by bit a binary

Reply to Message Icon

Name: jaivrat
Date: June 2, 2004 at 00:23:25 Pacific
OS: SunOS 5.8
CPU/Ram: NA
Comment:

Can Somebody help me in this.

How to make a c program so that if i give a filename to it as a command line argument, it tells the type of file it is?

e.g if i give it a core file, it says that it is a binary file, if some text file- it recognizes that is is a text file etc..

This has to be like unix command "file" which tells about the file passed as an argument.


Further, Can I have the same program to copy a binary file to another file.

FYI: I am trying to learn reading a file bit by bit and writing the same read bit to another file.


Jai Vrat Singh



Sponsored Link
Ads by Google

Response Number 1
Name: rohinkoul
Date: June 2, 2004 at 02:51:44 Pacific
Reply:

Hi there

Well one way of doing this is to scan whole file character by character and see if each character is a text character. If all characters are text characters including characters like \n , \t etc then it can be classified a a text file otherwise it is a binary file.

More over all file on disk are represented on disk in binary so there are no real text files. This distinction is made by the program acessing the file.

As for reading from file one bit at a time, I doubt you can do that since diskc are block devices and you have to read from them in blocks not bits. Though I am not very sure on this. One workaround may be to read a block into a buffer and then use bit operation on buffer elements to extract one bit at a time.

Can anyone throw some light on this????


0

Response Number 2
Name: StuartS
Date: June 2, 2004 at 04:42:19 Pacific
Reply:

I think what the OP meant was reading a file byte by byte. Files are normally read into a buffer in single block. The contents of the buffer can then be read byte by byte.

One way to decide if a file is a text file or a binary file is to read each byte. As soon as you hit a byte value greater than 127 you know you have a binary file and not an ASCII text file.

Stuart


0

Response Number 3
Name: wizard-fred
Date: June 3, 2004 at 12:43:29 Pacific
Reply:

I think Jaivrot is looking for a program that will read the first 'block' of a file, scan the contents and return the type of file. ex. 'MZ' for EXE, '%!EPS' For Encapsulared Postscript. In a manner similar to Windows, when you click on a file it either executes or starts the application associated with extension, except that it could not need the extension. I think there are programs that attempt to do this but none has the ability to identify all type of files.

By the way since your OS is SunOS are you trying to identify non-native applications.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C : read/write bit by bit a binary

VB6 Read/Write Binary File www.computing.net/answers/programming/vb6-readwrite-binary-file/1965.html

Read,seach and write a binary file? www.computing.net/answers/programming/readseach-and-write-a-binary-file/7204.html

reading/writing to memory in C www.computing.net/answers/programming/readingwriting-to-memory-in-c/6758.html