gpt4 book ai didi

c - 将文件的行逐字符读取到 char** 数组中

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

我编写了下一个函数,尝试读取文本文件中的每一行并将其输入到 c 中的字符串数组中:

int main(int argc,char* argv[])
{

char ** lines;
readFile(argv[1],lines);

}
int readFile(char* filePath,char** lines)
{
char file_char;
int letter_in_line=0;
int line=1;
char* line_string=malloc(1024);
int j=1;
int fd=open(filePath,O_RDONLY);
if (fd < 0)
{
return 0;
}

while (read(fd,&file_char,1) >0)
{
if(file_char != '\n' && file_char != '0x0')
{
line_string[letter_in_line] = file_char;
letter_in_line++;
}

else
{
if(lines != NULL)
{
lines=(char**)realloc(lines,sizeof(char*)*line);
}
else
{
lines=(char**)malloc(sizeof(char*));
}
char* line_s_copy=strdup(line_string);
lines[line-1]=line_s_copy;
line++;
letter_in_line=0;
memset(line_string,0,strlen(line_string));

}
j++;
}
printf("cell 0 : %s",lines[0]);

return 1;

}

我有 2 个问题:

  • 1)每当代码到达单元格 0 的打印时,我就会
    段错误(核心转储)错误。怎么了 ?
  • 2)如果我想要查看我的 main 中的lines数组的变化,我应该通过&lines 到 func 并获取 char*** 行作为参数?在
    另外,我需要将每个“line”关键字替换为“*line”?*我知道我可以使用fopen、fget等...我决定以这种方式实现它是有原因的。

最佳答案

有很多问题会导致您的代码出现核心转储。这里的版本与您的代码非常相似。我希望它能帮助你理解这一点。

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

int read_file(const char *filename, char ***result)
{

/* open the file */
const int fd = open(filename, O_RDONLY);
if (fd < 0) {
*result = NULL;
return -1;
}
/* read the file characters by characters */
char *buffer = (char *)malloc(sizeof(char) * 1024);
char c;
int column = 0;
int line = 0;
*result = NULL;

/* for each characters in the file */
while (read(fd, &c, 1) > 0) {
/* check for end of line */
if (c != '\n' && c != 0 && column < 1024 - 1)
buffer[column++] = c;
else {
/* string are null terminated in C */
buffer[column] = 0;
column = 0;

/* alloc memory for this line in result */
*result = (char **)realloc(*result, sizeof(char *) *
(line + 1));

/* duplicate buffer and store it in result */
(*result)[line++] = strdup(buffer);
}
}
free(buffer);
return line;
}

int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: %s [filename]", argv[0]);
return 1;
}

char **lines;
int line_count = read_file(argv[1], &lines);
if (line_count < 0) {
fprintf(stderr, "cannot open file %s\n", argv[1]);
return 1;
}

for(int i=0; i < line_count; i++)
printf("%s\n", lines[i]);

return 0;
}

这里是另一个版本:

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

int read_file(const char *filename, char ***result)
{
/* init result */
*result = NULL;

/* open the file */
FILE *file = fopen(filename, "r");
if (file == NULL)
return -1;

/* read the file line by line */
char *buffer = (char *)malloc(sizeof(char) * 1024);
int line = 0;
while (fgets(buffer, 1024, file)) {
*result = (char **)realloc(*result, sizeof(char *) *
(line + 1));
(*result)[line++] = strdup(buffer);
}
free(buffer);
return line;
}

int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: %s [filename]", argv[0]);
return 1;
}

char **lines;
int line_count = read_file(argv[1], &lines);
if (line_count < 0) {
fprintf(stderr, "cannot open file %s\n", argv[1]);
return 1;
}
for(int i=0; i < line_count; i++)
printf("%s\n", lines[i]);

return 0;
}

关于c - 将文件的行逐字符读取到 char** 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55659105/

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