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,349 of 243,242   
   bart to Kaz Kylheku   
   Re: is_binary_file()   
   06 Dec 25 12:42:53   
   
   From: bc@freeuk.com   
      
   On 06/12/2025 02:42, Kaz Kylheku wrote:   
   > On 2025-12-06, Michael Sanders  wrote:   
   >> 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) {   
   >   
   > [ ... ]   
   >   
   >>      fclose(f);   
   >>      return 0; // NOT binary   
   >> }   
   >   
   > How about:   
   >   
   > int is_binary_file(const char *path)   
   > {   
   >    FILE *f = fopen(path);   
   >    int yes = 0;   
   >   
   >    if (f) {   
   >      int ch;   
   >   
   >      while ((ch == getc(f)) != EOF) {   
   >        for (int i = 0; i < CHAR_BIT; i++, ch >>= 1) {   
   >          switch ((ch & 1)) {   
   >          case 0:   
   >          case 1:   
   >            break;   
   >          default:   
      
   If this is suppposed to detect files which don't consist of binary   
   characters (for example each ch has CHAR_BIT quaternary digits) then I   
   don't believe this will detect that.   
      
   Assumung that 'ch & 1' is equivalent to 'ch % 2' in that case.   
      
   --- 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