在C. Linux与Mac中使用文件功能的每个系统上的结果不同

时间:2015-07-01 23:37:58

标签: c linux macos gcc

我目前正在编写一个文件解析程序来解析一些数据。但是,它需要在Mac OS计算机上本机运行。但是,虽然代码在Ubuntu 14.04和Mac 10.10上运行良好。 Mac机器有时会提供一些垃圾数据。在linux上使用GCC可以随时提供正确的数据。

源数据如下,

[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

所需的输出如下。第一个数字是由行号确定的样本号。每个源行包含3个输出行。每个数字由4个十六进制字符表示。

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

然而,在我的Mac上,它的处理如下。

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

我尝试使用clang和gcc进行编译。也尝试使用c99。有时使用xcode编译我可以让它工作,但如果我从源文件夹移动它然后它再次中断。

以下代码源:

#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, the 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; i < 100; i++){
            if(currentLine[i] == '\0'){
                ///found end of string
                break;
            }
        }
        if(i < 60){
            ///line is short, so we assume it's a 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, the NULL pointer, on failure
                perror("Canot open file!!!\n");
                exit(-1);
            }
            continue;
        }

        for(i = 0; i < 100; i ++){
            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);
        i += 6;
        char j;
        for(j = 0; j < 3; j++){
            xNumber = readHexNumber(currentLine + i + (18 * j));
            yNumber = readHexNumber(currentLine + i + (18 * j) + 6 );
            zNumber = readHexNumber(currentLine + i + (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 is a carriage return. check the next character
            if((c = fgetc(inputFile)) == '\n'){
                    ///if it's a new line, we just eat the carriage return character
                fputc(c,interimFile);
            }else{
                    ///if the next character isn't a newline character, we make it so
                fputc('\n',interimFile);
                fputc(c,interimFile);
            }
        }else{
                ///put the character into the 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;
}

1 个答案:

答案 0 :(得分:1)

window.localstorage分配了4个字节。谁知道最后一个字符后面的字符。它有可能看起来像一个数字。设为numberSring并初始化numberString[5],告诉numberString[4] = 0停止的位置。