C: копирование текстового файла в связанный список приводит к исключению: выброшено исключение: нарушение прав чтения.ток был 0x2C2DE900 - PullRequest
0 голосов
/ 25 июня 2019

Изменить: Вопрос был обновлен для новой возникшей проблемы: |

Теперь, после исправления последней проблемы, я просто падаю на другую. После копирования кадра за головой. Функция «addFrame» выдает исключение, показанное в заголовке: (

Это текстовый файл, из которого я копирую

T1
C:\opencv\GIF Project\Photos\Tank_1.jpg
1000
T2
C:\opencv\GIF Project\Photos\Tank_2.jpg
1000
T3
C:\opencv\GIF Project\Photos\Tank_3.jpg
1000

Функция копирования:

/*
    loadGIF: This function loads the GIF from the text file
    Input: char filePath[STRING_SIZE = 50]: The path to the text file
    frameNode* head: The head of the linked list which we load the GIF into
    Output: None
*/
void loadGIF(char filePath[], frameNode** head)
{
    frameNode* temp = NULL;
    FILE* file = fopen(filePath, "r");
    char name[STRING_MAX] = { 0 }, path[STRING_MAX] = { 0 }, line[STRING_MAX] = { 0 };
    int counter = 0, sleep = 0, i = 0;
    while (fgets(line, sizeof(line), file))
    {
        if (counter == THREE)
        {
            counter = 0;
        }
        if (counter == 0)
        {
            strcpy(name, line);
            name[strcspn(name, "\n")] = 0;
        }
        if (counter == ONE)
        {
            strcpy(path, line);
            path[strcspn(path, "\n")] = 0;
        }
        if (counter == TWO)
        {
            sleep = line;
            atoi(sleep);
            if (i == 0)
            {
                *head = createFrame(name, path, sleep);
            }
            else
            {
                temp = createFrame(name, path, sleep);
                addFrame(&head, temp);
            }
            i++;
        }
        //memset(name, 0, sizeof(name));
        //memset(path, 0, sizeof(path));
        counter++;
    }
    fclose(file);
}

Часть основного, в которой я вызываю функцию:

int main(void)
{
    int i = 0, choise = -1, newGif = -1, sleep = 0, place = 0;
    char path[STRING_MAX] = { 0 }, name[STRING_MAX] = { 0 };
    frameNode* head = NULL, *temp = NULL;
    newGif = loadingFunction();
    if (newGif == 1)
    {
        printf("Enter The Path To The Text File Which Holds The GIF's Data: \n");
        fgets(path, STRING_MAX, stdin);
        path[strcspn(path, "\n")] = 0;
        loadGIF(path, &head);
    }
// The rest doesn't matter
}

Это функция addFrame:

/*
    addFrame: This function addes a new frame to the GIF
    Input: frameNode* head: The head of the linked list ( The first frame of the GIF )
           newFrame*: The new frame to be added
    Output: None
*/
void addFrame(frameNode* head, frameNode* newFrame)
{
    frameNode* current = head;
    while (current->next != NULL) // <-- Exception thrown here
    {
        current = current->next;
    }
    current->next = newFrame;
    newFrame->next = NULL;
}

Я полагаю, что проблема лежит где-то в создании связанного списка, но я не могу указать на это.

Я добавлю что-нибудь еще, что вам, ребята, нужно, если спросят. И я знаю, что вопросы, подобные этому, задавались ранее, я проверил их, и это, кажется, не помогает моему делу.

спасибо большое!

1 Ответ

2 голосов
/ 25 июня 2019

Это связано с тем, что counter продолжает увеличиваться и никогда не соответствует вашим условиям.

Прочитайте мои комментарии inline.

if (counter == 0)
{
    strcpy(name, line);
    name[strcspn(name, "\n")] = 0;
   counter++; //increment the counter **kiran**
}
if (counter == 1)
{
    strcpy(path, line);
    path[strcspn(path, "\n")] = 0;
   counter++; //increment the counter **kiran**
}
if (counter == 2)
{
    atoi(line);
    sleep = line;
    if (i == 0)
    {
        *head = createFrame(name, path, sleep);
    }
    else
    {
        temp = createFrame(name, path, sleep);
        addFrame(&head, temp);
    }
    counter = 0; //reset the counter
    i++;
}
    //memset(name, 0, sizeof(name));
    //memset(path, 0, sizeof(path));
   // counter++;   //remove this line    **kiran**

Обновление дообработать вторую проблему.

  addFrame(&head, temp);

должно быть

addFrame(*head, temp);

На данный момент вы передаете frameNode ***, но это должно быть frameNode *.

...