我必须创建一个C函数,它返回从文件描述符读取的行。我必须定义一个宏READ_SIZE
(可以编辑)。此READ_SIZE
表示每次调用read()时要读取的字符数。这个数字只能是正数。我还必须使用一个或多个静态变量来保存已读取但未发送回调用函数的字符。一个.C文件(最多5个函数,每个函数最多25行)和一个.h文件。我的函数GNL将在没有'\ n'的情况下返回它的返回值。如果在文件描述符上没有其他内容可读,或者在读取时发生错误,则该函数返回NULL。
这是函数的原型:
char *get_next_line(const int fd)
允许的功能:malloc
,free
,read
,write
(与my_putchar
,my_putstr
等一起使用)。
这是我到目前为止所做的,但实际上我没有任何想法如何启动它。如果你有任何线索让我开始,我真的很感激。如果必要的话,我将在接下来的十个小时内继续研究它。我会来这里查看并经常更新我的进度。如果您有任何疑问,请问我!
这是主文件(我在推动工作之前会删除):
#include "get_next_line.h"
void my_putchar(char c)
{
write(1, &c, 1);
}
int my_strlen(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
i++;
return (i);
}
char *my_putstr(char *str)
{
if (str == NULL)
write(1, "(null)", 6);
else
write(1, str, my_strlen(str));
return (str);
}
int main()
{
char *s;
while ((s = get_next_line(0)))
{
my_putstr(s);
my_putchar('\n');
free(s);
}
return (0);
}
到目前为止,这是我的get_next_line.c文件:
int check_backslash(char *buff)
{
int n;
n = 0;
while (buff[n])
{
if (buff[n] == '\n')
return (n);
n++;
}
return (0);
}
char *get_next_line(const int fd)
{
int n;
char *str_to_return;
static int i = 0;
static char buff[READ_SIZE];
n = 0;
while (n = 0)
{
if ((read(fd, buff, READ_SIZE)) == -1)
return (NULL);
n = check_backslash(buff);
}
return (str_to_return);
}
我已经做了一些我不会在这个问题上做的不同的事情,因为我想重新开始。我只需要一些线索来帮助我开始。再次感谢!