Different results on each system using file functions in C. Linux vs. Mac -


i writing file parsing program parse data. however, needs run natively on mac os machine. however, while code run fine on both ubuntu 14.04 , mac 10.10. mac machine give garbage data, sometimes. using gcc on linux gives correct data time.

the source data follows,

[6/30/2015 11:20:09 pm] stream begin [6/30/2015 11:20:09 pm] 00-00-ff-70-fc-a9-01-ec-ff-6c-fc-cc-01-fb-ff-70-fc-d4-01-fb [6/30/2015 11:20:09 pm] 00-01-ff-74-fc-c8-02-0b-ff-68-fc-b5-01-ff-ff-70-fc-b5-02-03 [6/30/2015 11:20:09 pm] 00-02-ff-6c-fc-b9-02-03-ff-6c-fc-d0-02-17-ff-64-fc-c4-01-f4 [6/30/2015 11:20:10 pm] 00-03-ff-68-fc-c4-01-ff-ff-59-fc-c8-01-ec-ff-5c-fc-e0-02-1b [6/30/2015 11:20:10 pm] 00-04-ff-5c-fc-dc-02-22-ff-5c-fc-d4-02-1e-ff-59-fc-c4-02-17 [6/30/2015 11:20:10 pm] 00-05-ff-60-fc-c4-02-13-ff-60-fc-c8-02-1b-ff-5c-fc-d0-02-1e [6/30/2015 11:20:10 pm] 00-06-ff-68-fc-d4-02-1e-ff-68-fc-c0-02-22-ff-60-fc-c4-02-0f [6/30/2015 11:20:10 pm] 00-07-ff-5c-fc-d0-02-03-ff-64-fc-b1-02-13-ff-59-fc-bd-01-ff 

the desired output follows. first number sample number determined line number. each source line contains 3 output lines. each number represented 4 hexadecimal characters.

1,-144,-855,492 2,-148,-820,507 3,-144,-812,507 4,-140,-824,523 5,-152,-843,511 6,-144,-843,515 7,-148,-839,515 8,-148,-816,535 9,-156,-828,500 10,-152,-828,511 11,-167,-824,492 12,-164,-800,539 13,-164,-804,546 14,-164,-812,542 15,-167,-828,535 16,-160,-828,531 17,-160,-824,539 18,-164,-816,542 19,-152,-812,542 20,-152,-832,546 21,-160,-828,527 22,-164,-816,515 23,-156,-847,531 

however, on mac, it's processing follows.

1,-144,-855,492 2,-2368,-13114,507 3,-2293,-812,507 4,-140,-824,523 5,-2432,-13482,511 6,-2293,-843,515 7,-148,-839,515 8,-2368,-13050,535 9,-2485,-828,500 10,-152,-828,511 11,-2672,-13178,492 12,-2613,-800,539 13,-164,-804,546 14,-2624,-12986,542 15,-2661,-828,535 16,-160,-828,531 17,-2560,-13178,539 18,-2613,-816,542 19,-152,-812,542 20,-2432,-13306,546 21,-2549,-828,527 22,-164,-816,515 23,-2496,-13546,531 24,-2661,-835,511 

i have tried compiling using clang, , gcc. tried using c99. using xcode compile can work, if move source folder breaks again.

code source following:

#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <fcntl.h> #include <errno.h> #include <unistd.h> #include <string.h>  static short readhexnumber(char * inputstring); static void processfile(file * inputfile); //main function int main (int argc, char **argv) {     char c;     char *input = argv[1];     file *input_file, *output_file;     char defaultout[] = "output000.csv";     char filenumber = 0;     char *filenumberstring;     filenumberstring = defaultout + 6;     input_file = fopen(input, "r");     output_file = fopen(defaultout, "w");     if (input_file == 0 || output_file == 0)     {         //fopen returns 0, null pointer, on failure         perror("canot open file!!!\n");         exit(-1);     }     else     {     processfile(input_file);     char currentline[100];     while (fgets(currentline,100,input_file))     {         unsigned short linenumber;         short xnumber;         short ynumber;         short znumber;          char isnewfile = 0;         char i;         for(i = 0; < 100; i++){             if(currentline[i] == '\0'){                 ///found end of string                 break;             }         }         if(i < 60){             ///line short, assume it's new stream             filenumber+=1;             char tempnumberstring[4] = "000";             sprintf(tempnumberstring, "%03d",filenumber);             filenumberstring[0] = tempnumberstring[0];             filenumberstring[1] = tempnumberstring[1];             filenumberstring[2] = tempnumberstring[2];             fclose(output_file);             output_file = fopen(defaultout, "w");             if(output_file == '\0'){                 //fopen returns 0, null pointer, on failure                 perror("canot open file!!!\n");                 exit(-1);             }             continue;         }          for(i = 0; < 100; ++){             c = currentline[i];             if(c == 0){                 ///reached end of line                 break;             }             if(c == ']'){                 ///found end of header                 i+=2;                 break;             }         }          linenumber = (unsigned short)readhexnumber(currentline + i);         += 6;         char j;         for(j = 0; j < 3; j++){             xnumber = readhexnumber(currentline + + (18 * j));             ynumber = readhexnumber(currentline + + (18 * j) + 6 );             znumber = readhexnumber(currentline + + (18 * j) + 12);             fprintf(output_file,"%d,%d,%d,%d\n",3*linenumber + j + 1, xnumber,ynumber,znumber);         }      }     }      fclose(input_file);     remove("interimfile");      return 0; } static void processfile(file * inputfile){     char * medianfile = "interimfile";     file * interimfile;     interimfile = fopen(medianfile,"w");     char c;     while((c = fgetc(inputfile)) != eof){         if(c == '\r'){                 ///if our character carriage return. check next character             if((c = fgetc(inputfile)) == '\n'){                     ///if it's new line, eat carriage return character                 fputc(c,interimfile);             }else{                     ///if next character isn't newline character, make                 fputc('\n',interimfile);                 fputc(c,interimfile);             }         }else{                 ///put character next file             fputc(c,interimfile);         }     }     fclose(interimfile);     fclose(inputfile);     inputfile = fopen(medianfile, "r"); } static short readhexnumber(char * inputstring){     char numberstring[4] = "0000";      numberstring[0] = inputstring[0]; //get msb     numberstring[1] = inputstring[1];//get next byte     numberstring[2] = inputstring[3]; //get next byte     numberstring[3] = inputstring[4];//get lsb      short number = (short)strtol(numberstring, null, 16);     return number; } 

there 4 bytes allocated numbersring. knows character follows last one. there possibility looks digit. make numberstring[5] , initialize numberstring[4] = 0, tell strtol stop.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -