gpt4 book ai didi

c - 从文件动态分配二维 double 组

转载 作者:行者123 更新时间:2023-11-30 15:04:35 25 4
gpt4 key购买 nike

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

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

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

#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;
}

最佳答案

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

#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;
}

关于c - 从文件动态分配二维 double 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40250924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com