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,354 of 243,242   
   Lew Pitcher to Scott Lurndal   
   Re: is_binary_file()   
   06 Dec 25 18:04:00   
   
   From: lew.pitcher@digitalfreehold.ca   
      
   On Sat, 06 Dec 2025 17:40:18 +0000, Scott Lurndal wrote:   
      
   > Kaz Kylheku <046-301-5902@kylheku.com> writes:   
   >>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);   
   >>   
   >>  if (f) {   
   >   
   >      while (isprint(getc(f)) {}   
      
   The isprint function tests for any member of a locale-specific   
   set of characters (each of which occupies one printing position   
   on a display device) including space (' ').   
      
   It effectively evaluates whether or not a given value is a   
   "printing character" in the execution characterset, not whether   
   or not a given value (from an outside file) is a text character.   
      
   I'd use this function cautiously, as it will produce false   
   results when the characterset of the source data is not the the   
   execution characterset (think a Unicode UTF16 encoded text   
   file, and an ASCII execution characterset).   
      
   >      return (!feof(f));   
   >   
   >    }   
   >    return 0;   
   >  }   
      
      
      
      
   --   
   Lew Pitcher   
   "In Skills We Trust"   
   Not LLM output - I'm just like this.   
      
   --- 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