简体   繁体   中英

Storing and unpacking a very long array of ints in C

I have a very long array of ints (3,000,000+) that I want to store and be able to unpack quickly. At first, I wrote each int on a separate line in a text file, but that took rather long to unpack as the strings had to be converted back to ints. Then, someone suggested that I just write the array itself to the file, but when I read it back and print out the elements, it only prints zeroes. The code looked something like this:

#include <stdio.h>
#include <stdlib.h>

#define LENGTH 3000000

int main() {
    FILE *f = fopen("file.txt", "w");
    int *arr = malloc(LENGTH * sizeof(int));

    /* initialization of arr not shown */

    fwrite(arr, sizeof(int), LENGTH, f);
    fclose(f);
    free(arr);
    return 0;
}

And then to unpack the data:

#include <stdio.h>
#include <stdlib.h>

#define LENGTH 3000000

int main() {
    FILE *f = fopen("file.txt", "r");
    int *arr = malloc(LENGTH * sizeof(int));
    fread(arr, sizeof(int), LENGTH, f);
    fclose(f);

    for (int i = 0; i < LENGTH; ++i) {
        printf("%d\n", arr[i]);
    }

    free(arr);
    return 0;
}

But, as I said, it only prints zeroes. Why does this not work, and is there a more appropriate way of doing such a thing?

Few things:

  1. When reading and writing the int s directly, you must open the file with the mode b as well:

     // Open for writing FILE *fp = fopen("myfile.bin", "wb"); // Open for reading: FILE *fp = fopen("myfile.bin", "rb");
  2. When calling malloc you should always check that the return value is not NULL to ensure that the allocation succeeded. When allocating 3000000 * sizeof(int) bytes, not checking for success is a disaster waiting to happen. You should also check the return values of fopen , fread , and fwrite :

     // Checking malloc for success arr = malloc(LENGTH * sizeof(int)); if(NULL == arr) { perror("malloc"); exit(EXIT_FAILURE); } // Checking fopen for success: FILE *fp = fopen("file.bin", "rb"); if(NULL == fp) { perror("fopen"); exit(EXIT_FAILURE); } // Checking fread for success: size_t readCnt = fread(arr, sizeof(int), LENGTH, fp); if(readCnt:= LENGTH) { // Did not read file successfully } // Checking fwrite for success, size_t writtenCnt = fwrite(arr, sizeof(int), LENGTH; fp); if(writtenCnt != LENGTH) { // Did not write file successfully }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM