使用fgets和strtok读入数据并创建链表

时间:2015-07-10 06:59:14

标签: linked-list fgets strtok

需要一些帮助,使用fgets和字符串标记化命令从文本文件中读取数据行,然后将其用于创建链接列表。我已经按照我在Stack Overflow和其他教程网站上找到的一些例子,但仍然无法获得下面的读取功能在我的程序中正常工作,它只会导致它崩溃。数据文件包含以下行:

Zucchini,Squash,pound,2.19,45 黄色,南瓜,磅,1.79,15

根据我读过的所有内容,我相信我有必要的代码,但显然我错过了一些东西。另外,我注释掉了其中一个字段(浮动价格的字段),因为我不知道用什么来复制数据中的浮点值,因为我不能把它当作字符串处理(下面的整数值)它似乎让我在我的编译器中逃脱它。)

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

// Struct for linked list node
struct produceItem
{
    char produce[20];
    char type[20];
    char soldBy[20];
    float price;
    int quantityInStock;
    struct produceItem *next;
};

// Function to read in data from file to
void read(struct produceItem **head)
{
    struct produceItem *temp = NULL;
    struct produceItem *right = NULL;

    //char ch[3];
    char line[50];
    char *value;

    FILE *data = fopen("RecitationFiveInput.txt", "r");

    printf("Trying to open file RecitationFiveInput.txt\n");

    if (data == NULL)
    {
        printf("Could not open file RecitationFiveInput.txt\n");
    }
    else
    {
        while(fgets(line, sizeof(line), data))
        {
            value = strtok(line, ", ");
            strcpy(temp->produce, strdup(value));

            value = strtok(NULL, ", ");
            strcpy(temp->type, strdup(value));

            value = strtok(NULL, ", ");
            strcpy(temp->soldBy, strdup(value));

            //value = strtok(NULL, ", ");
            //strcpy(temp->price, strdup(value));

            value = strtok(NULL, " \n");
            strcpy(temp->quantityInStock, strdup(value));

            temp->next = NULL;

            if (*head == NULL)
            {
                *head = temp;
            }
            else
            {
                right = *head;

                while(right->next != NULL)
                {
                    right = right->next;
                }

                right->next = temp;
            }

        }

        printf("Successfully opened file RecitationFiveInput.txt\n");
    }

    fclose(data);

    return;
}

// Function to display the nodes of the linked list that contains the data from the data file
void display(struct produceItem *head)
{
    int value = 1;

    struct produceItem *temp = NULL;

    temp = head;

    printf("=============================================================================\n");
    printf(" Item #   Produce          Type          Sold By          Price      In Stock\n");
    printf("=============================================================================\n");

    if(temp == NULL)
    {
        return;
    }
    else
    {
        while(temp != NULL)
        {
            printf(" %d   %s          %s          %s          %lf      %d\n", value, temp->produce, temp->type, temp->soldBy, temp->price, temp->quantityInStock);
            value++;

            temp = temp->next;

            if(temp == NULL)
            {
                break;
            }
        }
    }
    return;
}

//Main function
int main()
{
    int input = 0;

    struct produceItem *head = NULL;

    while(1)
    {
        printf("\nList Operations\n");
        printf("=================\n");
        printf("1. Stock Produce Department\n");
        printf("2. Display Produce Inventory\n");
        printf("3. Reverse Order of Produce Inventory\n");
        printf("4. Export Produce Inventory\n");
        printf("5. Exit Program\n");
        printf("Enter your choice: ");

        if(scanf("%d", &input) <= 0)
        {
            printf("Enter only an integer.\n");
            exit(0);
        }
        else
        {
            switch(input)
            {
                case 1:
                    read(&head);
                    break;
                case 2:
                    display(head);
                    break;
                case 3:
                    //function
                    break;
                case 4:
                    //function
                    break;
                case 5:
                    printf("You have exited the program, Goodbye!\n");
                    return 0;
                    break;
                default:
                    printf("Invalid option.\n");
            }
        }
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

别介意所有人,发现了这个问题。崩溃是由于我没有为read me函数中的临时指针分配内存。