[英]How to create an array of strings given an array of pointers?
I am given a text file and I need to put it in a buffer and use get_lines to make an array of pointers after converting each line to a string. 给了我一个文本文件,我需要将其放在缓冲区中,并在将每一行转换为字符串后使用get_lines组成一个指针数组。 I am having trouble with just the get_lines function as I am getting a seg fault when run it.
我在使用get_lines函数时遇到了麻烦,因为在运行它时出现了段错误。
Here's my code: 这是我的代码:
#include <stdlib.h>
#include <stdio.h>
int readfile(FILE *fp, char **cbuf);
char **get_lines(char *cbuf, int bufsize, int word);
int readword(FILE*tmp);
int main(int argc, char *argv[])
{
int i,bufsize, num_word;
char *cbuf;
char **lines;
FILE *fp;
FILE *tmp;
if( (fp=fopen(argv[1],"r")) == NULL)
{
perror("ERROR: bad/no filename");
exit(0);
}
tmp = fopen(argv[1],"r");
bufsize = readfile(fp,&cbuf);
num_word = readword(tmp);
lines = get_lines(cbuf, bufsize, num_word) ;
i=0;
while( lines[i] != NULL) {
printf("%i\t%s\n",i,lines[i]);
i++;
}
return 0;
}
int readfile(FILE *fp,char**cbuf)
{
int i;
char c;
fseek(fp, 0L, SEEK_END);
int bufsize = ftell(fp);
fseek(fp, 0L, SEEK_SET);
*cbuf = (char *)malloc(sizeof(char) * bufsize);
for (i = 0; i < bufsize; i++)
{
c = fgetc(fp);
(*cbuf)[i] = c;
}
return bufsize;
}
int readword(FILE*tmp)
{
int word = 0;
char c;
while((c = fgetc(tmp)) != EOF )
{
if (c == '\n')
word++;
}
return word;
}
char **get_lines(char *cbuf, int bufsize, int word)
{
int i = 0, j = 0, counter = 0;
char (*lines)[bufsize];
lines = (char**)malloc(sizeof(char*)*bufsize);
counter = cbuf;
for (i = 0; i < bufsize; i++)
{
if(cbuf[i] == '\n')
{
cbuf[i] == '\0';
counter = cbuf[i + 1];
j++;
}else
{
*lines[j] = &counter;
}
}
lines[word] == NULL;
return lines;
}
The violation causing the fault is not immediately obvious to me, can someone tell me what might be wrong in get_lines()? 引起错误的违规行为对我来说不是立即显而易见的,有人可以告诉我get_lines()中可能有什么问题吗?
This code is wrong: 这段代码是错误的:
char (*lines)[bufsize];
lines = (char**)malloc(sizeof(char*)*bufsize);
It allocates a pointer to an array of char. 它分配一个指向char数组的指针。 Then you malloc the wrong amount of space, cast to the wrong type, and write
*lines[j] = &counter;
然后,您将分配错误的空间量,将其转换为错误的类型,然后编写
*lines[j] = &counter;
which tries to store a pointer in a char
. 试图将指针存储在
char
。
You should get many compiler errors/warnings for the get_lines
function. 您应该为
get_lines
函数获得许多编译器错误/警告。 It's important to pay attention to such messages as they are telling you that something is wrong with your code. 请注意这些消息,因为它们告诉您代码有问题,这一点很重要。 There's no point even starting to investigate a segfault until you have fixed all the errors and warnings.
在修复所有错误和警告之前,甚至没有必要开始调查段错误。
See here for a great guide on how to debug your code; 有关如何调试代码的出色指南, 请参见此处 。 I suspect you would fail the rubber duckie test on the
get_lines
function. 我怀疑您会在
get_lines
函数上的橡皮鸭测试失败。
Here is a fixed version (untested): 这是固定版本(未经测试):
// Precondition: cbuf[bufsize] == '\0'
//
char **get_lines(char *cbuf, size_t bufsize, size_t num_lines)
{
// +1 for the NULL termination of the list
char **lines = malloc((num_lines + 1) * sizeof *lines);
size_t line = 0;
while ( line < num_lines )
{
lines[line++] = cbuf;
cbuf = strchr(cbuf, '\n');
if ( !cbuf )
break;
*cbuf++ = '\0';
}
lines[line] = NULL;
return lines;
}
In your existing code there is no room to write the null terminator for the last line; 在您现有的代码中,没有空间为最后一行写空终止符。 my advice is to make
readfile
actually malloc
one extra byte and make sure that is set to 0
. 我的建议是使
readfile
实际上malloc
一个额外的字节,并确保将其设置为0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.