I am trying to write my file into a structure but when I compile the copy does not seem to have worked. I don't want to use fread or scanf ... I just want to use read. Here is my code:
Structure :
typedef struct s_dict_entry
{
char **nbr;
char *name;
} t_dict_entry;
Function to add File to Structure :
int add_struct_file(char *file, int ac)
{
t_dict_entry mydict;
char *buff;
int i;
int j;
int fd;
i = 0;
j = 0;
fd = 0;
buff = malloc(sizeof(char) * 100);
fd = open(file, O_RDONLY);
if (fd == -1)
return (-1);
mydict = *(t_dict_entry*)malloc(sizeof(t_dict_entry) * (ac + 1));
mydict.nbr = (char**)malloc(sizeof(char*) * 100);
while (read(fd, &buff, 1) && *buff != '\n')
{
if (*buff >= '0' && *buff <= '9')
{
mydict.nbr[i] = (char*)malloc(sizeof(char) * 100);
while (read(fd, &buff, 1) && (*buff >= '0' && *buff <= '9'))
mydict.nbr[i++][j] = *buff;
}
}
close(fd);
printf("Dict entry = %s\n", mydict.nbr[i]);
return (0);
Return:
Dict entry = (null)
File :
0: Cherry
1: Banana
2: Apple
3: Apricot
4: Pear
5: Peach
6: Plum
7: Watermelon
8: Melon
9: Quince
10: Lemon
Thank you for your answers !