嗯,我正在尝试创建一个程序,它从文件描述符中逐段读取(定义为 BUFFER_SIZE),直到找到 ' ' 或 EOF。问题是我陷入了无限循环,我尝试解决原因但没有成功(您可以注意到 printf 遍布代码)。注意:对于这个作业,我只允许使用有限数量的标准函数,因此要使用 realloc,我必须实现自己的函数,与 strlen 和其他函数相同。 (假设它们是正确的)。允许的功能:写入、读取、MALLOC 和释放。有代码:
void *ft_realloc(void *ptr, size_t oldsize, size_t size)
{
void *new;
size_t minsize;
if (size == 0)
{
free(ptr);
return (NULL);
}
if (ptr == NULL)
return (malloc(size));
new = malloc(size);
if (!new)
return (NULL);
minsize = size;
if (oldsize < size)
minsize = oldsize;
memcpy(new, ptr, minsize);
while (size - oldsize != 0)
((char *)new)[oldsize++] = '\0';
free(ptr);
return (new);
}
int find_newline_eof(char *buffer)
{
int i;
i = 0;
if (!buffer)
return (-1);
while (buffer[i])
{
if (buffer[i] == '\n')
return (i);
i++;
}
return (-1);
}
char *extract_line(char **buffer, int newline_pos)
{
char *line;
int remaining_len;
//in terms of params, to newline be -1, means that we reach EOF
without '\n'
if (!*buffer)
return (NULL);
if (newline_pos == -1)
newline_pos = ft_strlen(*buffer);
line = malloc(newline_pos + 1);
if (!line)
return (NULL);
memcpy(line, *buffer, newline_pos);
line[newline_pos] = '\0';
if ((*buffer)[newline_pos] == '\n')
newline_pos++;
printf("reached EXTRACT_LINE, line: (%s)\n", line);
return (NULL); // The function reaches HERE, the error is beyond.
remaining_len = ft_strlen(*buffer + newline_pos);
// memmove safer against overlapping than memcpy
memmove(*buffer, *buffer + newline_pos, remaining_len + 1);
*buffer = ft_realloc(*buffer, ft_strlen(*buffer), remaining_len + 1);
if (!*buffer)
{
free(line);
return (NULL);
}
return (line);
}
int read_newpiece(int fd, char **buffer)
{
int bytes_readed;
char current_buffer[BUFFER_SIZE + 1];
char *new_buffer;
bytes_readed = read(fd, current_buffer, BUFFER_SIZE);
if (bytes_readed <=0) //SAFE CHECKS
return (bytes_readed);
current_buffer[bytes_readed] = '\0';
//printf("entered here!\n");
//return (bytes_readed);
if (!*buffer)
{
*buffer = malloc(1);
if (!*buffer)
return (-1);
(*buffer)[0] = '\0'; // Initialize as an empty string
}
new_buffer = ft_realloc(*buffer, ft_strlen(*buffer),
ft_strlen(*buffer) + bytes_readed + 1);
if (!new_buffer)
return (-1);
*buffer = new_buffer;
strncat(*buffer, current_buffer, bytes_readed);
return (bytes_readed);
}
char *get_next_line(int fd)
{
static char *buffer = NULL;
char *line; //returned value
int newline_index; //position where we find the EOF or '\n';
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
if (!buffer)
{
buffer = malloc(1);
if (!buffer)
return (NULL);
buffer[0] = '\0';
}
newline_index = -1; //-1 (NOT FOUND)
while (newline_index == -1 && read_newpiece(fd, &buffer) > 0)
{
printf("buffer after read: /%s/\n", buffer);
newline_index = find_newline_eof(buffer);
printf("newline index: %d\n", newline_index);
//return (NULL);
}
if (newline_index == -1 && !buffer)
return (NULL);
line = extract_line(&buffer, newline_index);
printf("extracted line: ///%s///\n", line);
return (NULL);
}
int main(int ac, char **av)
{
int fd;
char *line;
if (ac < 2)
return (write(1, "Usage: ./a.out <filename>\n", 27));
fd = open(av[1], O_RDONLY);
if (fd <= -1) //SAFE CHECKS
return (write(1, "error opening file\n", 20));
while ((line = get_next_line(fd)) != NULL)
{
printf("%s\n", line);
free (line);
}
close (fd);
return (0);
}
重新分配需要三个参数:原始缓冲区、该缓冲区的长度和新的所需大小。如果大小>长度,它将整个缓冲区复制到一个新缓冲区,如果大小< length. I appreciate any suggestion on any aspect of the code, but mainly to resolve the problem
,则复制(大小)个字符我尝试借助 printf 逐步识别问题,以检查某些上下文中的值
get_next_line()
每次调用时都会返回“”,因此无限循环位于 main()
:
while ((line = get_next_line(fd)) != NULL) {
printf("%s\n", line);
free (line);
}
这是因为
read_newpiece()
永远不会将任何内容分配给 *buffer
,而无条件 return bytes_readed
位于函数中间。