Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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????

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

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.

![]() |
![]() |
![]() |

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