c - isspace : Need to identify only space not other characters -


i have piece of code sas calling c program read/write vsam file .

char *ltrim(char *s) {         while(isspace(*s)) s++;      return s;     }  char *rtrim(char *s) {          char* = s + strlen(s);      while(isspace(*--back));      *(back+1) = '\0';      return s;     }  char *trim(char *s) {         return rtrim(ltrim(s));     } 

it called way

strlen(trim(xxxx)) 

values 99999899.99 present in xxxx. written comp-3 filed in vsam file. in hex value written output file ending 0x99,0x89,0x90,0x0c.

i found issue program removes 0c in trim function. rtrim function when value 99999899.00. if value 99999899.99 no issue observed. if value less 99999899.00 no issue observed. found quite strange till read isspace function considers below ( http://www.cplusplus.com/reference/cctype/isspace/ ):

  • ' ' (0x20) space (spc)
  • '\t' (0x09) horizontal tab (tab)
  • '\n' (0x0a) newline (lf)
  • '\v' (0x0b) vertical tab (vt)
  • '\f' (0x0c) feed (ff)
  • '\r' (0x0d) carriage return (cr)

i pretty sure author looking remove spaces. in short, want remove spaces , not (0x0c) feed (ff) . can replace these 2 lines

while(isspace(*--back)); *(back+1) = '\0'; 

replace isspace(x) x == ' '

btw, rtrim function causes undefined behaviour if string consists of spaces (or string empty), run off front of array. might want re-design it, or @ least make stop when hits front.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -