从文件读取数据到结构中 [英] Read data from file into structure

查看:130
本文介绍了从文件读取数据到结构中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个可以处理链表/节点中的结构项目的程序。
我的大部分函数运行良好,但是我坚持如何从一个txt文件读取到一个结构(readFromFile函数)。
我一直在阅读,但我仍然很困惑,主要是如何写这个函数,而不是在main中,也阅读到一个结构

任何帮助,将不胜感激。

编辑:
我似乎无法得到在此刻的工作答案的功能,所以我正在改变尝试使程序从主要的txt读取。
我可以打开这个文件,但问题是:
如何将数据读入我的链表?



(代码已被修改)

我正在阅读的文本文件格式如下:




 #1扁平螺丝刀
12489
36
.65
1.75
#2扁平螺丝刀
12488
24
.70
1.85
#1菲利普斯螺丝刀
12456
27
0.67
1.80
#2十字螺丝刀
12455
17
0.81
2.00
羊角锤
03448
14
3.27
4.89
Tack Hammer
03442
9
3.55
5.27
十字切割
07224
6
6.97
8.25
Rip锯b $ b 07228
5
6.48
7.99
6可调扳手
06526
11
3.21
4.50






远:

  #includestdafx.h
#include< stdlib.h>

typedef结构库存
{
char invName [36];
int invPartNo;
int invQOH;
浮动invUnitCost;
浮动invPrice;
}股票;

struct NODE
{
union
{
int nodeCounter;
void * dataitem;
}项;
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);

$ b $ int main(int argc,char * argv [])
{
struct NODE *
header = InitList();

char ch,file_name [25];
FILE * fp;

printf(输入你希望看到的文件名称\\\
);
获取(file_name);

fp = fopen(file_name,r); //读取模式

if(fp == NULL)
{
perror(打开文件时出错。
exit(EXIT_FAILURE);


printf(%s文件的内容是:\ n,file_name); ((ch = fgetc(fp))!= EOF)


{
//在这里放什么?
}

fclose(fp);
DisplayList(header);



return 0;

$ b $ struct struct NODE * InitList()
{$ b $ struct NODE * temp =(struct NODE *)malloc(sizeof NODE);

temp-> item.nodeCounter = 0;
temp-> link = NULL;
return temp;


$ b 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 ++;


$ b $ struct struct NODE * GetNode(FILE * fptr)
{
struct NODE * temp =(struct NODE *)malloc(sizeof NODE) ;

temp-> item.dataitem = ReadData(fptr);
temp-> link = NULL;

return temp;


$ b void DisplayList(struct NODE * start)
{
struct NODE * current = start-> link;

while(current!= NULL)
{
DisplayNode((struct inventory *)current-> item.dataitem);
current = current->链接;




void DisplayNode(struct inventory * stuff)
{
/ *
char invName [36];
int invPartNo;
int invQOH;
浮动invUnitCost;
浮动invPrice;
* /

printf(Name:%s,stuff-> invName);
printf(Part Number:%d,stuff-> invPartNo);
printf(现有数量:%d,stuff-> invQOH);
printf(单位成本:%0.2f,stuff-> invUnitCost);
printf(Price%0.2f,stuff-> invPrice);


$ b结构库存* 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(输入物品数量:);
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;

$ b $ struct NODE * SearchList(struct NODE * start,int oldData)
{$ b $ struct NODE * current = start;
struct inventory * st =(struct inventory *)current-> link-> item.dataitem; (st-> invPartNo!= oldData&& current!= NULL)


{
current = current-> link;
if(current-> link)
st =(struct inventory *)current-> link-> item.dataitem;
}
返回当前;

$ b $ void DeleteNode(struct NODE * start,int oldData)
{
struct NODE * current,* oldNode;

current = SearchList(start,oldData);
oldNode = current->链接;
current-> link = oldNode->链接;
free(oldNode);
start-> item.nodeCounter - = 1;


解决方案

$ c $> fscanf()和 fgets()读取数据,

  void readFromFile()
{
stock array [20];
int i,j;
i = 0;
fp = fopen(input.txt,r); ($!
if(fp!= NULL){
while(!feof(fp)){
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 ++;




$ 数组[] 将有数据, i 将是读取的结构数据的数量。

这里 fscanf()被放在跳过[enter]字符,因为数据是在不同的行中。
read about fscanf() code>> fgets()>它可以帮助读取文件。
避免 feof()
$ $ $ $ $ $ $ $ $ ,fp)){
fscanf(fp,%d%d%f%f,& array [i] .invPartNo,& array [i] .invQOH,& array [i] .invUnitCost ,&安培;阵列[I] .invPrice);
i ++;
}

用于这种特定的文件格式。

I am working on a program that can process structure items in linkedlist/nodes. I have most of the functions running fine, however am stuck on how to read from a txt file into a structure (the readFromFile function). I have been reading around but am still quite confused, mainly on how to write this as a function instead of in main, and also reading into a structure

any help would be appreciated.

EDIT: I cannot seem to get the functions in the answers to work at the moment, so I am changing to trying to make the program read from txt in main. I could open the file, but the problem is: How do I read data into my linked list?

(code has been modified)

The text file I am reading from is formatted like this:


#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


My program so far:

#include "stdafx.h"
#include <stdlib.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();

    char ch, file_name[25];
   FILE *fp;

   printf("Enter the name of file you wish to see\n");
   gets(file_name);

   fp = fopen(file_name,"r"); // read mode

   if( fp == NULL )
   {
      perror("Error while opening the file.\n");
      exit(EXIT_FAILURE);
   }

   printf("The contents of %s file are :\n", file_name);

   while( ( ch = fgetc(fp) ) != EOF )
   {
      //what to put here?
   }

   fclose(fp);
   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)
{
    /*
    char invName[36];
    int  invPartNo;
    int  invQOH;
    float invUnitCost;
    float invPrice;
    */

    printf("Name: %s", stuff->invName);
    printf("Part Number: %d", stuff->invPartNo);
    printf("Quantity on hand: %d", stuff->invQOH);
    printf("Unit Cost: %0.2f", stuff->invUnitCost);
    printf("Price %0.2f", 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;
}

解决方案

You can use fscanf() and fgets() to read the data as,

void readFromFile( )
{
   stock array[20];
   int i,j;
   i=0;
   fp = fopen("input.txt", "r");
   if( fp != NULL ){
      while ( !feof(fp ) ){
         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++;
      }
}

The array[] will have the data and i will be number of struct data read.
Here all spaces in fscanf() are put to skip the [enter] character since the data is in different lines.
read about fscanf() with its format matching and fgets() it can help in reading files.
To avoid feof() can use,

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++;   
}

for This particular file format.

这篇关于从文件读取数据到结构中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆