1

I have a project to finish ...... and for this i need a char ***a to get all the words that reffer to the dictionary names from a text file (for a translator). My program always breakpoints on the strcpy(a[line][v[i]], p); I'm stuck numar_cuvinte = word_number linie = line

while (!feof(dictionary_list))
{
    fgets(buffer, 100, dictionary_list);
    p = strtok(buffer, "-.\n");
    while (p != NULL)
    {

        if (p != "txt")
        {
            nr_cuvinte++;
            p = strtok(NULL, "-.\n");
        }
        else
            p = NULL;
    }
    v[linie] = nr_cuvinte;  // ---------> v[i] = nr cuvinte pe linie 
    nr_cuvinte = 0;// ----->m
    linie++; // --->n
}
//-------------------------------------------------------------------------
a = (char***)malloc(linie*sizeof(char**));

for (int i = 0; i < linie; i++)
{
    a[i] = (char**)malloc(v[i] * sizeof(char*));
}

for (int i = 0; i < linie; i++)
{
    for (int j = 0; j < v[i]; j++)
    {
        a[i][j] = (char*)malloc(20 * sizeof(char));
    }
}
//--------------------------------------------------------------------------
rewind(dictionary_list);
linie = 0;
nr_cuvinte = 0;
while (!feof(dictionary_list))
{
    fgets(buffer, 100, dictionary_list);
    p = strtok(buffer, "-.\n");
    while (p != NULL)
    {
        if (p != "txt")
        {

            strcpy(a[linie][v[nr_cuvinte]], p);
            nr_cuvinte++;
            p = strtok(NULL, "-.\n");
        }
        else p = NULL;

    }
    v[linie] = nr_cuvinte;
    nr_cuvinte = 0;// ----->m
    linie++; // --->n
}

}

Rares Andrei
  • 81
  • 1
  • 10
  • 1
    Aside: please change `while(!feof(dictionary_list))` to `while(fgets(buffer, 100, dictionary_list) != NULL)` and read [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Weather Vane Jan 29 '17 at 20:09
  • Wellcome to SO. In fact, this is a question and answer site, there is no question there. Please go through the help text in the top bar to see what kind of questions we would be happy to answer, and also on how to ask them. – Jens Gustedt Jan 29 '17 at 20:09
  • `if (p != "txt")` does not do what you think. It will never be the same pointer. Also there is no need to mess with the pointer returned from `strtok`, it is a simple enough loop without interfering. – Weather Vane Jan 29 '17 at 20:13
  • is `nr_cuvinte` initialized with `0` before entering the `while`-loop? Same for `linie`? Are you sure that `p` in your `strcpy` never exceeds 19 characters + terminating 0? – Stephan Lechner Jan 29 '17 at 20:30
  • `a[linie][v[nr_cuvinte]]` looks like it should be `a[linie][nr_cuvinte]` – user253751 Jan 29 '17 at 21:15

0 Answers0