将值0写入已分配的内存,返回大值

时间:2015-01-30 18:12:07

标签: c file-io

short * left = malloc(sizeof(short));
short * right = malloc(sizeof(short));
short * combined = malloc(sizeof(short));
FILE * inputFile = fopen(inputf, "r");;
FILE * outputFile = fopen(outputf, "w");;

while(1){
    int resultLeft = fread(left, sizeof(short), 1, inputFile);
    int resultRight = fread(right, sizeof(short), 1, inputFile);

    //if we reached the end of the file, close files and break the loop.
    if (resultLeft != 1 || resultRight != 1){
        fclose(inputFile);
        fclose(outputFile);
        break;
    }

    //*combined = operation;

    fwrite(combined, sizeof(short), 1, outputFile);
    fwrite(combined, sizeof(short), 1, outputFile);
}

我已经在这里工作了好几个小时,还有其他人看过它,但我无法弄清楚这一点。要点是我需要读取短裤对(纯字节),使用操作组合它们,然后将组合两次写入新文件。唯一的问题是这里的fread函数将值“0”写入左右分配的内存中。

代码中的早期示例fread(headerbuffer, 11 * sizeof(int), 1, inputFile);工作正常,导致更多混乱。

0 个答案:

没有答案