Связанные списки со сложными вложенными структурами несовместимая ошибка указателя - PullRequest
0 голосов
/ 25 марта 2020

Эта программа имитирует простую файловую систему. Реализовано односвязными списками

Exemple input: [terminal]

create fs
touch 123.c 123
mkdir folder
touch 111.c text
mkdir folder 2
delete fs

Я получаю эту ошибку, когда я [файл g cc. c -o файл]: * ​​1004 *

file.c: In function ‘append’:
file.c:34:14: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
         last = last->next;
              ^
file.c:37:16: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
     last->next = new_node;

Код указан ниже;

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "file.h"

void createFS(Directory* myFileSystem);
void readString(char* operationToBeExecuted);

void append(struct ListOfFiles** head_ref, char *name, char *data) 
{ 
    /* 1. allocate node */
    struct ListOfFiles* new_node = (struct ListOfFiles*) malloc(sizeof(struct ListOfFiles)); 

    struct ListOfFiles *last = *head_ref;  /* used in step 5*/

    /* 2. put in the data  */
    new_node->anyFile->name = name;
    new_node->anyFile->data = data;
    new_node->anyFile->size = strlen(data); 

    /* 3. This new node is going to be the last node, so make next of 
          it as NULL*/
    new_node->next = NULL; 

    /* 4. If the Linked List is empty, then make the new node as head */
    if (*head_ref == NULL) 
    { 
       *head_ref = new_node; 
       return; 
    } 

    /* 5. Else traverse till the last node */
    while (last->next != NULL) 
        last = last->next; 

    /* 6. Change the next of last node */
    last->next = new_node; 
    return; 
} 

int main() {
    char *operationToBeExecuted = (char*)malloc(101 * sizeof(char));
    printf("What to do now?\n");
    readString(operationToBeExecuted);

    while (strcmp(operationToBeExecuted, "create fs") != 0) {
        printf("Please type \"create fs\" to create your FileSystem!\n");

        readString(operationToBeExecuted);
    }

    // Create our Parent Directory 
    Directory* Directory = NULL;
    Directory = (struct Directory*)malloc(sizeof(struct Directory));
    createFS(Directory);

    readString(operationToBeExecuted);
    while(strcmp(operationToBeExecuted, "") == 0) {
        printf("Please enter a Proper Value!\n");
        readString(operationToBeExecuted);
    }
    while (strcmp(operationToBeExecuted, "delete fs") != 0) {

        char *tempString = strtok(operationToBeExecuted, " ");

/////////////////////////////////////////////////////////////////////////////
        if (strcmp(tempString, "touch") == 0) {

            char *fileName = strtok(NULL, " ");
            char *content = strtok(NULL, " ");
            int lenght = strlen(content);

            // struct ListOfFiles* newNode = (struct ListOfFiles*)malloc(sizeof(struct ListOfFiles));

            Directory->myFiles = malloc(sizeof(*(Directory->myFiles)));
            Directory->myFiles->anyFile = malloc(sizeof(*(Directory->myFiles->anyFile)));

            // struct ListOfFiles *last = Directory->myFiles;

           append(&Directory->myFiles, fileName, content);

            // // 2. put in the data
            // newNode->anyFile->name = fileName;
            // newNode->anyFile->data = content;
            // newNode->anyFile->size = lenght;

            // // 3. new node is going to be NULL
            // newNode->next = NULL;

            // // if (Directory->myFiles == NULL) {
            // //     Directory->myFiles = newNode;
            // // } else {
            // //     while (last->next == NULL) {
            // //         last = last->next;
            // //     }
            // //     last->next = newNode;
            // }


            // // 2. put in the data
            // Directory->myFiles->anyFile->name = fileName;
            // Directory->myFiles->anyFile->data = content;
            // Directory->myFiles->anyFile->size = lenght;

            // // 3. new node is going to be NULL
            // Directory->myFiles->next = NULL;





            printf("%s - ", Directory->myFiles->anyFile->name);
            printf("%s - ", Directory->myFiles->anyFile->data);
            printf("%d\n", Directory->myFiles->anyFile->size);
//////////////////////////////////////////////////////////////////////////////  
        } else if (strcmp(tempString, "mkdir") == 0) {

            Directory->myDirectories = malloc(sizeof(*(Directory->myDirectories)));
            Directory->myDirectories->anyDirectory = malloc(sizeof(*(Directory->myDirectories->anyDirectory)));

            char *directoryName = strtok(NULL, " ");
            printf("%s\n", directoryName);

            // Directory->myDirectories->anyDirectory = NULL;
            Directory->myDirectories->anyDirectory->name = directoryName;
            // Directory->myDirectories->anyDirectory->myDirectories = NULL;
            // Directory->myDirectories->anyDirectory->myFiles = NULL;
            Directory->myDirectories->anyDirectory->parentDir = Directory;

            // printf("%s\n", Directory->myDirectories->anyDirectory->name);
            printf("Wow. Let;s MKDIR!\n");
        } else {
            printf("Invalid operator! Please enter again!\n");
        }
        readString(operationToBeExecuted);
        // char *tempString = strtok(operationToBeExecuted, " ");
        // while (tempString != NULL) {
        //     printf("%s\n", tempString);
        //     tempString = strtok(NULL, " ");
        // }
    }

    printf("You have successfully deleted your FileSystem!\n");
    free(operationToBeExecuted);
    free(Directory->myDirectories->anyDirectory);
    free(Directory->myDirectories);
    free(Directory->myFiles->anyFile);
    free(Directory->myFiles);
    free(Directory);
}

void createFS(Directory* myFileSystem) {
    myFileSystem->name = "/";
    myFileSystem->parentDir = NULL;
    printf("%s\n", myFileSystem->name); // test
}

void readString(char* operationToBeExecuted) {
    fgets(operationToBeExecuted, 101, stdin);
    operationToBeExecuted[strlen(operationToBeExecuted) - 1] = '\0';
}
#ifndef __FILE_H__
#define __FILE_H__


typedef struct Directory {
    // The name of the directory
    char *name;

    // TODO: The list of files of the current directory
    struct ListOfFiles {
        struct File *anyFile;
        struct File *next;
    } *myFiles;

    // TODO: The list of directories of the current directory
    struct ListOfDirectories {
        struct Directory *anyDirectory;
        struct Directory *next;
    } *myDirectories;

    // The parent directory of the current directory (NULL for the root directory)
    struct Directory *parentDir;
} Directory;

// DO NOT MODIFY THIS STRUCTURE
typedef struct File {
    // The name of the file
    char *name;

    // The size of the file
    int size;

    // The content of the file
    char *data;

    // The directory in which the file is located
    Directory *dir;
} File;

#endif /* __FILE_H__ */

1 Ответ

3 голосов
/ 25 марта 2020

У вас есть:

struct ListOfFiles {
    struct File *anyFile;
    struct File *next;
} *myFiles;

struct ListOfDirectories {
    struct Directory *anyDirectory;
    struct Directory *next;
} *myDirectories;

Вам необходимо:

struct ListOfFiles {
    struct File *anyFile;
    struct ListOfFiles *next;
} *myFiles;

struct ListOfDirectories {
    struct Directory *anyDirectory;
    struct ListOfDirectories *next;
} *myDirectories;

Возможно, есть и другие проблемы, которые необходимо решить (некоторые из них могут сообщаться компилятором, другие - нет быть обнаруженным до времени выполнения), но это должно исправить проблему с заголовком.

...