从文件动态分配2d数组的双精度数

时间:2016-10-25 22:43:30

标签: c arrays

所以我有一个随机数的文件,如下所示:

2 3 1 
1 2 3 4 5 6
1 2 3 4 5
1 2 
1 

我的任务是创建一个二维的二维数组。对于每个double,它只分配所需的空间(长度为double) 到目前为止,在我的代码中,我已经扫描了文件,并设法读取字符串文件,但我想知道我将如何为longs实现这一点。

#include <stdio.h>      /* For printf and file management */
#include <stdlib.h>     /* For dynamic memory allocation */
#include <string.h>     /* For string functions */
/* 
* Read all lines from text file, and store them in a dynamically 
* allocated array. 
* The count of lines is stored in the 'count' output parameter
* The caller is responsible for freeing the allocated memory.
 */
char** read_lines(FILE* txt, int* count) {
char** array = NULL;        /* Array of lines */
int    i;                   /* Loop counter */
char   line[100];           /* Buffer to read each line */
int    line_count;          /* Total number of lines */
int    line_length;         /* Length of a single line */

/* Clear output parameter. */
*count = 0;

/* Get the count of lines in the file */
line_count = 0;
while (fgets(line, sizeof(line), txt) != NULL)  
{                                      
   line_count++;
}

/* Move to the beginning of file. */
rewind(txt);

/* Allocate an array of pointers to strings 
 * (one item per line). */
array = malloc(line_count * sizeof(char *));
if (array == NULL) {
    return NULL; /* Error */
}

/* Read each line from file and deep-copy in the array. */
for (i = 0; i < line_count; i++) {    
    /* Read the current line. */
    fgets(line, sizeof(line), txt);

    /* Remove the ending '\n' from the read line. */
    line_length = strlen(line);        
    line[line_length - 1] = '\0';
    line_length--; /* update line length */

    /* Allocate space to store a copy of the line. +1 for NUL terminator */
    array[i] = malloc(line_length + 1);

    /* Copy the line into the newly allocated space. */
    strcpy(array[i], line);
}

/* Write output param */
*count = line_count;

/* Return the array of lines */
return array;
}

1 个答案:

答案 0 :(得分:1)

您还需要保存每行中的元素数量 像这样:

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

typedef double Type;
#define SCAN_FORMAT "%lf"
#define PRINT_FORMAT "%g"

typedef struct jagged_rows {
    size_t n; //number of elements
    Type *elements;
} JaggedArray_row;

typedef struct jagged_array {
    size_t n; //number of rows
    JaggedArray_row *rows;
} JaggedArray;

JaggedArray *load(FILE *file){
    JaggedArray *ja = malloc(sizeof(*ja));
    if(ja == NULL){
        perror("malloc");
        exit(EXIT_FAILURE);
    }
    ja->n = 0;
    ja->rows = NULL;

    int ch, has_value = 0;
    size_t rows = 0, cols = 0;
    while((ch = getc(file)) != EOF){
        if(isspace(ch)){
            if(ch == '\n' && has_value){
                ++rows;
                has_value = cols = 0;
            }
            continue;
        }
        ungetc(ch, file);
        if(has_value){
            ja->rows[rows].elements = realloc(ja->rows[rows].elements, ++ja->rows[rows].n * sizeof(Type));//check omitted
            fscanf(file, SCAN_FORMAT, &ja->rows[rows].elements[cols++]);//check omitted
        } else {
            has_value = 1;
            ja->rows = realloc(ja->rows, ++ja->n * sizeof(*ja->rows));//check omitted
            ja->rows[rows].n = 0;
            ja->rows[rows].elements = malloc(++ja->rows[rows].n * sizeof(Type));//check omitted
            fscanf(file, SCAN_FORMAT, &ja->rows[rows].elements[cols++]);//check omitted
        }
    }
    return ja;
}

int main(void) {
    JaggedArray *ja = load(stdin);
    for(int r = 0; r < ja->n; ++r){
        for(int c = 0; c < ja->rows[r].n; ++c){
            if(c)
                putchar(' ');
            printf(PRINT_FORMAT, ja->rows[r].elements[c]);
        }
        puts("");
        free(ja->rows[r].elements);
    }
    free(ja->rows);
    free(ja);
    return 0;
}