Originally Posted by
john.c
I don't understand your question. Please post a complete program and a more specific question.
Sorry for not being clear!
Here is the full code:
Code:
#include "line_by_line.h"
#include <stdio.h>
static int check_line(char *str)
{
int i;
i = 0;
while (str)
{
if (str[i] == '\n')
return (1);
i++;
}
return (0);
}
static char *ft_trim_line(char *str)
{
char *ret;
int i;
i = 0;
while (str[i] != '\n')
i++;
ret = ft_strnew(i);
i = 0;
while (str[i] != '\n')
{
ret[i] = str[i];
i++;
}
ret[i] = '\0';
return (ret);
}
static char *ft_prep_next(char *str)
{
int i;
char *ret;
i = 0;
while (str[i] != '\n')
i++;
ret = ft_strsub(str, i + 1, ft_strlen(str) - 1);
return (ret);
}
int line_by_line(const int fd, char **line)
{
char buf[BUFF_SIZE + 1];
char *str;
char *tmp;
char c;
ssize_t ret;
str = ft_memalloc(1);
while ((ret = read(fd, buf, BUFF_SIZE)) > 0)
{
if (ret == -1)
return (-1);
buf[BUFF_SIZE] = '\0';
str = ft_strjoin(str, buf);
if (check_line(str))
{
*line = ft_trim_line(buf);
//printf("BUF IS\n\n<<%s>>\n\n",buf);
//*buf = *buf - 3;
//tmp = ft_prep_next(buf);
ft_strclr(buf);
//buf = ft_strdup(tmp);
return(1);
}
}
if (ret == 1)
return (1);
// if (ret == EOF)
// return (0);
else
return (-1);
}
Lets say my BUFF_SIZE is 8, in my first read, I am reading into the second line.. The next time I call the function line_by_line, the function returns only part of the second line..
My main is:
Code:
#include "line_by_line.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
/*int main(int argc, char **argv)
{
int i;
int fd;
char *line;
line = NULL;
i = 0;
argc = 0;
if (!(fd = open(argv[1], O_RDONLY)))
return (0);
while ((i = line_by_line(fd, &line)) > 0)
{
printf("%s\n", line);
}
if (close(fd) == -1)
{
return (0);
}
return (0);
}*/
int main(int argc, char **argv)
{
char *line;
int fd;
int code;
int len;
int i;
i = 1;
printf("BUFF_SIZE = %d\n", BUFF_SIZE);
while (i < argc)
{
if ((fd = open(argv[i], O_RDONLY)) == -1)
printf("Error: Can't open file");
while ((code = line_by_line(fd, &line)) > 0)
{
len = ft_strlen(line);
printf("%d - %d - |%s|\n", code, len, line);
free(line);
}
i++;
if (i < argc)
}
printf("LAST %d - |%s|\n", code, line);
return (0);
}
I'm using my own static library too..