0

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 !

kira
  • 23
  • 2
  • `*(t_dict_entry*)malloc(sizeof(t_dict_entry) * (ac + 1))` is wrong for two reasons: First because you [should not cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc); Ane second because it's a memory leak. – Some programmer dude Oct 25 '20 at 08:02
  • Then I don't see why `nbr` needs to be a pointer to a pointer to `char`. Should it be a string containing the number in the file? – Some programmer dude Oct 25 '20 at 08:04
  • Lastly, I recommend you read the full lines into a buffer (you don't need to allocate it dynamically, use arrays, so you don't have to worry about more memory leaks). Once you have the full line in the buffer use e.g. [`strtok`](https://en.cppreference.com/w/c/string/byte/strtok) to separate the number and the name. – Some programmer dude Oct 25 '20 at 08:06
  • Ok I'm so lost .. Is there an easier way to copy a file to a structure or array with read ()? If so could you show me a little example? – kira Oct 25 '20 at 08:22
  • I recommend you start by dividing the complex problem (creating an array of structures, where the data of each structure is read from a file) into smaller and simpler problems. Continue this division of the less complex problems into even lesser complex problems, until they can't be subdivided more. The smallest problems will no longer be complex, but rather simple and should be easy to solve one by one. Do that, putting them together to solve the more complex problems until you have solved your original problem. I also recommend you use many more functions, maybe even one per simple problem. – Some programmer dude Oct 25 '20 at 09:15
  • For example, reading the file. Create a function to read one line at a time from the file. This file could then be divided into reading a single character from the file, checking if the newly read single character was a newline (or if there was an error/end of file), adding the single character to a buffer. Return a status indicator if the line was read successfully or not. Then call this function in a loop until it fails. When you have done that, you have the code to read, line by line, the whole file. Then continue with the next sub-problem: Parsing the lines. Etc. – Some programmer dude Oct 25 '20 at 09:18

0 Answers0