home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 242,344 of 243,242   
   Michael Sanders to All   
   is_binary_file()   
   06 Dec 25 01:05:44   
   
   From: porkchop@invalid.foo   
      
   Am I close? Missing anything you'd consider to be (or not) needed?   
      
      
      
   /*   
    * Checks if a file is likely a binary by examining its content   
    * for NULL bytes (0x00) or unusual control characters.   
    * Returns 0 if text, 1 if binary or file open failure.   
    */   
      
   int is_binary_file(const char *path) {   
       FILE *f = fopen(path, "rb");   
       if (!f) return 1; // cannot open file, treat as error/fail check   
      
       unsigned char buf[65536];   
       size_t n, i;   
      
       while ((n = fread(buf, 1, sizeof(buf), f)) > 0) {   
           for (i = 0; i < n; i++) {   
               unsigned char c = buf[i];   
      
               // 1. check for the NULL byte (strong indicator of binary data)   
               if (c == 0x00) {   
                   fclose(f);   
                   return 1; // IS binary   
               }   
      
               // 2. check for C0 control codes (0x01-0x1F), excluding known   
               // text formatting characters: 0x09 (Tab), 0x0A (LF), 0x0D (CR)   
               if (c < 0x20) {   
                   if (c != 0x09 && c != 0x0A && c != 0x0D) {   
                       fclose(f);   
                       return 1; // IS binary (contains unexpected control code)   
                   }   
               }   
           }   
       }   
      
       fclose(f);   
       return 0; // NOT binary   
   }   
      
   --   
   :wq   
   Mike Sanders   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca