File

This is a discussion on File within the C Programming forums, part of the General Programming Boards category; How we can find the given file is Binary or Text File with the help of c program?????...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    2

    File

    How we can find the given file is Binary or Text File with the help of c program?????

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, a text-file is a binary file too - since everything stored in a computer is binary.

    It is non-trivial to determine if something is text or not, but reading the file (in binary form) and scanning to see if all (or nearly all) of it's content is "printable characters", newline or carriage return would be one way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Could you not just search for 0 bytes?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by zacs7 View Post
    Could you not just search for 0 bytes?
    Is there a guarantee that zero bytes are always present in a binary? I don't think so. In code, yes. In the file produced by this:
    Code:
    int main()
    {
       unsigned char a[255];
       FILE *f;
       for(i = 0; i < 255; i++)
          a[i] = i+1;
       f = fopen("blah.bin", "w");
       fwrite(a, sizeof(a), 1, f);
       fclose(f);
    }
    It wouldn't print very well tho'.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    As said before all files are binary file. It totally depends on how do u open a file. So for example if can create a text file with some text. And you can open the same text file in binary to in a text.

    But if you create a binary file and open in a text mode, may be u wont recognize those chars. Since it might all me random chars. It up you on how u open the file and how u interpret the values in the file.

    ssharish

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How we can find the given file is Binary or Text File with the help of c program?????
    Binary or text are C's way of knowing how to properly buffer characters for you. That's it. The data coming in is binary, and the data going out is binary. In the middle you might get textual conversions like CRLF to LF (and vice versa). Since the difference is only relevant within your program, and your program opened the file, you already know what the open mode was.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 09:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21