From: lew.pitcher@digitalfreehold.ca   
      
   On Sat, 06 Dec 2025 19:06:14 +0000, Scott Lurndal wrote:   
      
   > Lew Pitcher writes:   
   >>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.   
   >   
   > What is your definition of a "text" character?   
      
   I have none, for this case. However, the OP /might/ have one, given   
   that his code was an attempt to discern "text" files from "binary"   
   files.   
      
   >>   
   >>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)   
|