gpt4 book ai didi

c - 从文件中动态分配二维字符串数组

转载 作者:太空宇宙 更新时间:2023-11-03 23:27:45 24 4
gpt4 key购买 nike

这个问题可能很常见,但我没有找到任何接近这个确切问题的东西,所以我会问。我有一个看起来像这样的文件。

ABCSTRX
ABCST
ABCS
ABCSTRXY

我的任务是创建二维字符串数组。对于每个字符串,它只分配需要的空间(字符串的长度),所以它看起来像这样。

the red area is for '\0'

到目前为止,在我的代码中,我已经扫描了文件并得到了代表文件中字符串数量的数字。

void function(FILE *txt){
char arr[50];
int counter=0;
char **array;

do {
fgets(arr,MAX,txt);
counter++;
} while(fgets(arr,MAX,txt)!=NULL);

rewind(txt);
array = malloc(counter * sizeof(char*));


//FROM NOW ON THE CODE IS NOT CORRECT
do {
fgets(arr,MAX,txt);
counter++;

for(int j=0;j<strlen(arr); j++){
array[i]= malloc(strlen(arr) * sizeof(char));
array[i]= arr;
}
i++;
} while(fgets(arr,MAX,txt)!=NULL);

}

问题是我找不到解决方案,如何用大小不匹配的字符串填充动态分配的数组。这是我想出的代码,我觉得卡住了。在数组中填满字符串后,它将被打印出来(我想有两个 for 循环)。感谢评论

最佳答案

您可能需要考虑以下带注释的可编译 C 代码。
我没有在这里写一些注释,而是在代码中插入了一些内联注释,以试图清楚地解释代码的作用。

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

int main(int argc, char * argv[]) {
char** array = NULL; /* Array of read lines */
FILE* file = NULL; /* File to read lines from */
const char* filename = NULL; /* Name of the input file */
int i; /* Loop index */
int line_count; /* Total number of read lines *7

/* Get filename from the command line. */
if (argc != 2) {
printf("Specify the input file from the command line.\n");
return 1;
}
filename = argv[1];

/* Try opening the file. */
file = fopen(filename, "rt");
if (file == NULL) {
printf("Can't open file %s.\n", filename);
return 1;
}

/* Read lines from file. */
array = read_lines(file, &line_count);

/* Just for test, print out the read lines. */
for (i = 0; i < line_count; i++) {
printf("[%d]: %s\n", (i+1), array[i]);
}

/* Cleanup. */
fclose(file);
for (i = 0; i < line_count; i++) {
free(array[i]);
}
free(array);
array = NULL;

/* All right */
return 0;
}

使用这样的输入文件:

ABCSTRX
ABCST
ABCS
ABCSTRXY

输出是:

C:\Temp>test.exe testfile.txt
[1]: ABCSTRX
[2]: ABCST
[3]: ABCS
[4]: ABCSTRX

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

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