来自bin文件的fread()整数数组

时间:2014-10-10 21:52:36

标签: c arrays fread bin

我想读取二进制文件的内容,并将其存储到数组中。二进制文件有一组整数,我需要从一个特定的位置读取它,直到数组中存储了一定数量的整数。

int array[atoi(argv[1])] ;   
for (i = 0; i < atoi(argv[1]);i++){
            array[i] = rand() %100;
            printf(" %d ", array[i]);
        } // Creates an array filled with random numbers

        fp = fopen("array","w+");
        fwrite(array, sizeof(array), sizeof(array), fp);
        fclose(fp); // Creates the bin file and close it.

// In this example the bin file has 20 integers, and I want to read from the 5th integer till the 16th integer
    int b[15]; 
    int i;
    FILE *fl;
    fl = fopen("array","rb");
    fseek(fl, 5, SEEK_SET);
    for (i = 0; i < 12; i++){
        fread(&b[i], sizeof(int),1, fl);
        fl++;
        printf(" %d ", b[i]);
    }

最后,我应该将所有整数存储在每个单元格中的b数组中,但我无法使其工作。

0 个答案:

没有答案