将文件中的文本逐行插入到指针传递的字符串数组中的函数 [英] Function to insert text line by line from a file into string array passed by pointer
问题描述
我正在尝试创建一个函数read_lines,该函数需要一个文件* fp,一个指向char **行的指针以及一个指向int num_lines的指针.该函数应将每一行文本插入到行中,并将num_lines增加到文件包含的行数.
I'm trying to create a function read_lines that takes a file *fp, a pointer to char** lines, and pointer to int num_lines. The function should insert each line of text into lines, and increase num_lines to however many lines the file has.
这可能真的很简单,但是我已经尝试插入文本几个小时了.
Its probably really simple but I've been trying to insert the text for several hours now.
这是main.c的外观.除了read_lines之外的所有东西都已经定义并且可以正常工作.
This is what main.c would look like. Everything but read_lines is already defined and working.
int main(int argc, char* argv[]){
char** lines = NULL;
int num_lines = 0;
FILE* fp = validate_input(argc, argv);
read_lines(fp, &lines, &num_lines);
print_lines(lines, num_lines);
free_lines(lines, num_lines);
fclose(fp);
return 0;
}
这是我尝试添加行的尝试之一,但我无法弄清楚.
This is one of my attempts at trying to append lines, but I couldn't figure it out.
read_lines.c
read_lines.c
void read_lines(FILE *fp, char ***lines, int *num_lines) {
int i;
int N = 0;
char s[200];
for (i=0; i<3; i++)
{
while(fgets(s, 200, fp)!=NULL){N++;}
char strings[50][200];
rewind(fp);
fgets(s, 200, fp);
strcpy(lines[i],s);
}
}
感谢您为解决此问题提供的帮助.
I'd appreciate any help at solving this, thanks.
推荐答案
一种解决方案(不包含标头和用于可读性的错误检查):
A solution (without headers and error checking for readability):
void read_lines(FILE *stream, char ***lines_ptr, size_t *num_lines_ptr) {
char **lines = NULL;
size_t num_lines = 0;
char *line = NULL;
size_t len = 0;
ssize_t nread;
while ((nread = getline(&line, &len, stream)) != -1) {
lines = lines == NULL
? malloc(sizeof(char*))
: realloc(lines, (num_lines+1)*sizeof(char*));
lines[num_lines] = malloc(nread+1);
memcpy(lines[num_lines], line);
++num_lines;
}
free(line);
*lines_ptr = lines;
*num_lines_ptr = num_lines;
}
完整解决方案:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// lines_ptr: Output. Initial value ignored. To be freed by caller on success.
// num_lines_ptr: Output. Initial value ignored.
// Returns: 0 on error (errno set). 1 on success.
int read_lines(FILE *stream, char ***lines_ptr, size_t *num_lines_ptr) {
char ***lines = NULL;
size_t num_lines = 0;
char *line = NULL;
size_t len = 0;
ssize_t nread;
while ((nread = getline(&line, &len, stream)) != -1) {
char **new_lines = lines == NULL
? malloc(sizeof(char*))
: realloc(lines, (num_lines+1)*sizeof(char*));
if (new_lines == NULL)
goto error;
lines = new_lines;
lines[num_lines] = malloc(nread+1);
if (lines[num_lines] == NULL)
goto error;
memcpy(lines[num_lines], line);
++num_lines;
}
if (ferror(stream))
goto error;
free(line);
*lines_ptr = lines;
*num_lines_ptr = num_lines;
return 1;
error:
for (size_t i=num_lines; i--; )
free(lines[i]);
free(lines);
free(line);
*lines_ptr = NULL;
*num_lines_ptr = 0;
return 0;
}
(您可以使用..._ptr
变量来保存三行,而不必在末尾设置它们,但这真的值得您付出可读性吗?)
(You could save three lines by using the ..._ptr
vars instead of setting them at the end, but is that really worth the readability cost?)
这篇关于将文件中的文本逐行插入到指针传递的字符串数组中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!