将文本读入C中的链表

时间:2014-04-22 05:39:05

标签: c text

我正在尝试编写一个可以将文本文件读入链接列表的程序。 然而,虽然程序确实读取了内容,但是当我打印列表时,值已经关闭。

  1. 始终删除名称的第一个字符。

  2. 显示列表时,整数和浮点值不正确。

  3. 我一直在研究这个问题很长时间,但无法弄清楚问题。任何帮助表示赞赏。

    这是我正在阅读的文件:

    #1 Flat Blade Screwdriver
    12489
    36
    .65
    1.75
    #2 Flat Blade Screwdriver
    12488
    24
    .70
    1.85
    #1 Phillips Screwdriver
    12456
    27
    0.67
    1.80
    #2 Phillips Screwdriver
    12455
    17
    0.81
    2.00
    Claw Hammer
    03448
    14
    3.27
    4.89
    Tack Hammer
    03442
    9
    3.55
    5.27
    Cross Cut Saw
    07224
    6
    6.97
    8.25
    Rip Saw
    07228
    5
    6.48
    7.99
    6" Adjustable Wrench
    06526
    11
    3.21
    4.50
    

    这是我的代码:

    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <stddef.h>
    
    typedef struct inventory
    {
        char invName[36];
        int  invPartNo;
        int  invQOH;
        float invUnitCost;
        float invPrice;
    }stock;
    
    struct  NODE
    {
        union
        {
            int  nodeCounter;
            void  *dataitem;
        }item;
        struct NODE *link;
    };
    
    struct NODE *InitList();
    void DisplayNode(struct inventory *);
    struct inventory * ReadData(FILE *);
    void DisplayList(struct NODE *);
    struct NODE* GetNode(FILE *);
    void  Add2List(struct NODE *, struct NODE *);
    struct NODE* SearchList(struct NODE *, int );
    void  DeleteNode(struct NODE *, int );
    
    
    int main(int argc, char* argv[])
    {
        struct NODE *header;
        header = InitList();
    
        int i, j;
        i = 0;
        FILE *fp = fopen("input.txt", "r");
    
        //remove \n char from end of lines
    
    
        if( fp != NULL )
        {
            while(!feof(fp))
            {
                struct NODE *nNode =  (struct NODE*)malloc(sizeof NODE);
                struct inventory *newNode =  (struct inventory*)malloc(sizeof inventory);
    
                fgets(newNode->invName, 100, fp);
                fscanf(fp, " %d %d %f %f ", &newNode->invPartNo,&newNode->invQOH,&newNode->invUnitCost,&newNode->invPrice);
                //fscanf(fp,"%s %d %d %f %f ", newNode->invName, &newNode->invPartNo,&newNode->invQOH,&newNode->invUnitCost,&newNode->invPrice);
                nNode->item.dataitem = newNode;
                nNode->item.nodeCounter++;
                Add2List(header, nNode);
            }
         }
        DisplayList(header);
    
        return 0;
    }
    
    struct NODE *InitList()
    {
        struct NODE *temp = (struct NODE*)malloc(sizeof NODE);
    
        temp->item.nodeCounter = 0;
        temp->link = NULL;
        return temp;
    }
    
    
    void  Add2List(struct NODE *start, struct NODE *NewNode)
    {
        struct NODE *current = start;
    
        while (current->link != NULL)
            current = current->link;
    
        current->link = NewNode;
        NewNode->link = NULL;
    
        start->item.nodeCounter++;
    }
    
    
    struct NODE* GetNode(FILE *fptr)
    {
        struct NODE *temp = (struct NODE*)malloc(sizeof NODE);
    
        temp->item.dataitem = ReadData(fptr);
        temp->link = NULL;
    
        return temp;
    }
    
    
    void DisplayList(struct NODE *start)
    {
        struct NODE *current = start->link;
    
        while (current != NULL)
        {
            DisplayNode((struct inventory *)current->item.dataitem);
            current = current->link;
    
        }
    }
    
    
    void DisplayNode(struct inventory *stuff)
    {
        printf("Name: %s\n", stuff->invName);
        printf("Part Number: %d\n", stuff->invPartNo);
        printf("Quantity on hand: %d\n", stuff->invQOH);
        printf("Unit Cost: %0.2f\n", stuff->invUnitCost);
        printf("Price %0.2f\n\n", stuff->invPrice);
    }
    
    
    struct inventory * ReadData(FILE *fptr)
    {
        struct inventory *temp = (struct inventory *)malloc(sizeof inventory);
    
        if(fptr==stdin)
            printf("Enter item name: ");
        fscanf_s(fptr, "%s", temp->invName);
        if(fptr==stdin)
            printf("Enter item part number: ");
        fscanf_s(fptr, "%d", &temp->invPartNo);
        if(fptr==stdin)
            printf("Enter item quantity on hand: ");
        fscanf_s(fptr, "%d", &temp->invQOH);
        if(fptr==stdin)
            printf("Enter item unit cost: ");
        fscanf_s(fptr, "%f", &temp->invUnitCost);
        if(fptr==stdin)
            printf("Enter item price: ");
        fscanf_s(fptr, "%f", &temp->invPrice);
    
        return temp;
    }
    
    struct NODE* SearchList(struct NODE *start, int oldData)
    {
        struct NODE* current = start;
        struct inventory * st = (struct inventory *)current->link->item.dataitem;
    
        while (st->invPartNo != oldData && current != NULL)
        {
            current = current->link;
            if(current->link)
                st = (struct inventory *)current->link->item.dataitem;
        }
        return current;
    }
    
    void  DeleteNode(struct NODE *start, int oldData)
    {
        struct NODE *current, *oldNode;
    
        current = SearchList( start, oldData);
        oldNode = current->link;
        current->link = oldNode->link;
        free(oldNode);
        start->item.nodeCounter -= 1;
    }
    
    void readFile()
    {
        stock array[20]; //9 items to read from list
        int i, j;
        i = 0;
        FILE *fp = fopen("input.txt", "r");
        if( fp != NULL )
        {
            while(fgets(array[i].invName, sizeof array[i].invName, fp))
            {
                fscanf(fp,"%d %d %f %f ",&array[i].invPartNo,&array[i].invQOH,&array[i].invUnitCost,&array[i].invPrice);
                i++;
            }
         }
    
    }
    

1 个答案:

答案 0 :(得分:2)

这个

        nNode->item.dataitem = newNode;
        nNode->item.nodeCounter++;
        Add2List(header, nNode);

没有机会工作,我猜这是主要问题。您的itemunion

union
{
    int  nodeCounter;
    void  *dataitem;
}item; 

union is a special data type available in C that enables you to store different data types in the same memory location.这意味着您将newNode的地址分配到nNode->item.dataitem

        nNode->item.dataitem = newNode;

你破坏了这里的地址

        nNode->item.nodeCounter++; 

我猜您的情况item应该是struct

相关问题