0

我有一个 double[n][20],其中 n 可能高达 200。如何将其存储到文件中?

4

3 回答 3

2

我要么用 ANSI-C 编写自己的文件写入例程,要么使用 NSArray 写入文件功能。取决于偏好。

保存一个 NSArray

我实际上写了一段代码来确保它可以在 iOS 平台上运行。我将在这里发布洞的东西,你可以选择你想要的。本质上,我创建了一个 5 x 5 的静态数组进行测试,但它适用于任何大小的数组。这提供了用于以二进制格式写入文件(最快的方法和保留原始双精度的最佳方法)和读回文件的代码。如您所见,我在 UIViewController 的 viewDidLoad 例程中实现了主要代码。我用随机数填充了数组(不像我用静态 79 播种 rand 例程那样)。

在我定义的头文件中

double values[5][5];


- (void)viewDidLoad {
    [super viewDidLoad];

    NSBundle * myBundle = [NSBundle mainBundle];
    NSString * absPath  = [myBundle resourcePath];
    NSString * currPath;

    srand(79);

    FILE * aFile;
    currPath=[absPath stringByAppendingString: @"/myfile.bin"];
    if( NULL == ( aFile = fopen([currPath cStringUsingEncoding:[NSString defaultCStringEncoding]],"rb") ) ){

        //Generate some random doubles
        printf("generating 5 x 5 random values\r\n");
        for(int i = 0; i < 5; i++){

            for(int j = 0; j < 5; j++){
                values[i][j] = (double)rand() / (double)rand();
                printf("%.3f, ", values[i][j]);
            }
            printf("\r\n");
        }

        aFile = fopen([currPath cStringUsingEncoding:[NSString defaultCStringEncoding]],"wb");
        fwrite (values , 1 , sizeof(double) * 5 * 5 , aFile );
        fclose(aFile);

    }else{
        printf("File myfile.bin exists.  Reading in File.\r\n");
        fread(&values, sizeof(double), 5 * 5, aFile);
        for(int i = 0; i < 5; i++){

            for(int j = 0; j < 5; j++){
                printf("%.3f, ", values[i][j]);
            }
            printf("\r\n");
        }
        fclose(aFile);
    }

}

代码的输出(在控制台窗口中如下所示)

generating 5 x 5 random values
0.002, 2.969, 0.298, 1.137, 0.790,
8.753, 0.884, 0.264, 0.034, 1.033, 
3.848, 1.049, 1.350, 0.402, 2.192, 
4.284, 0.979, 0.162, 1.294, 0.339, 
0.830, 0.175, 0.633, 1.008, 1.142, 

[Session started at 2012-06-02 03:56:44 +0000.]
File myfile.bin exists.  Reading in File.
0.002, 2.969, 0.298, 1.137, 0.790, 
8.753, 0.884, 0.264, 0.034, 1.033, 
3.848, 1.049, 1.350, 0.402, 2.192, 
4.284, 0.979, 0.162, 1.294, 0.339, 
0.830, 0.175, 0.633, 1.008, 1.142, 

希望这可以帮助。

于 2012-06-02T01:59:34.650 回答
1

Try something like this:

void writeData(double[][20] data, int n, const char name[]) {
    FILE* output = fopen(name, "w");
    int i, j;
    fprintf(output, "%d\n", n);
    for(i = 0; i < n; i++) {
        for(j = 0; j < 20; j++) {
            fprintf(output, "%lf\n", data[i][j]);
        }
    }
}

double** readData(const char name[]) {
    FILE* input = fopen(name, "r");
    int i, j, n;
    fscanf(input, "%d", &n);
    double** ret = (double**)malloc(n*sizeof(double*));
    for(int i = 0; i < n; i++) {
        ret[i] = (double*)malloc(20*sizeof(double));
        for(int j = 0; j < 20; j++) {
            fscanf(input, "%lf", &ret[i][j]);
        }
    }
    return ret;
}

FYI, this is untested code, but it should give you an idea of how to do this. This is simply a text file with the first line containing the length (n), and the rest of the lines containing the doubles.

You could also use a plist, because the tags can hold doubles. Use NSNumber*s to write the doubles, nested within s.

void writeData(double** data, int n, const char name[]) {
    FILE* output = fopen(name, "w");
    int i, j;
    fprintf(output, "%d\n", n);
    for(i = 0; i < n; i++) {
        for(j = 0; j < LPCC_DIM; j++) {
            fprintf(output, "%lf\n", data[i][j]);
        }
    }
}

double** readData(const char name[], int* getN) {
    FILE* input = fopen(name, "r");
    int n;
    fscanf(input, "%d\n", &n);
    *getN = n;
    double** ret = (double**)malloc(n*sizeof(double*));
    for(int i = 0; i < n; i++) {
        ret[i] = (double*)malloc(LPCC_DIM*sizeof(double));
        for(int j = 0; j < LPCC_DIM; j++) {
            fscanf(input, "%lf", &ret[i][j]);
        }
    }
    return ret;
}
于 2012-06-02T02:10:13.690 回答
1
#include <fcntl.h> // for open(), close()
#include <unistd.h> // for write()

int fd = open(path, O_WRONLY|O_BINARY); // system call
write(fd, d, sizeof(double)*n*20);
close(fd);

http://www.manpagez.com/man/2/open/

http://www.manpagez.com/man/2/write/

于 2012-06-02T01:58:35.883 回答