将数据从文件读入结构

我正在开发一个可以处理链表/节点中的结构项的程序。 我有大多数函数运行正常,但是我仍然坚持如何从txt文件读取到一个结构(readFromFile函数)。 我一直在阅读但仍然很困惑,主要是关于如何将其作为一个函数而不是在main中编写,并且还读入一个结构

任何帮助,将不胜感激。

编辑:我似乎无法在目前的答案中获得function,所以我正在尝试从主要的txt中读取程序。 我可以打开文件,但问题是:如何将数据读入我的链表?

(代码已被修改)

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


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

您可以使用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++; } } 

array[]将拥有数据, i将读取结构数据的数量。
这里fscanf()中的所有空格都跳过[enter]字符,因为数据在不同的行中。
阅读fscanf()及其格式匹配和fgets()它可以帮助读取文件。
为了避免feof()可以使用,

 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. 以“文本”“读取”模式打开文本文件。
  2. 如果您事先知道项目数,则创建一个stock结构数组,否则使用每个类型stock节点的链接列表。
  3. 要从文件读取数据到相应的字段,您可以使用fscanf
  4. 当您认为已完成数据读取时,请关闭该文件。

笔记:

  1. 文件指针会自动递增,因此您无需担心将其递增以读取下一组数据。
  2. 别忘了关闭文件。
  3. 要检查文件处理函数是否正常工作,请检查所有函数的返回值。

重要:

由于文件中的invPartNo字段具有以0开头的整数数据,因此您可能不希望将其作为整数读取,否则将其视为“八进制数”而不是十进制数。

您的_tmain可能如下所示,此代码假定文件中的内容已完成(即包含正确的行数):

 int _tmain(int argc, _TCHAR* argv[]) { struct NODE *header = InitList(); if (argc > 1) { FILE *f; if ((f = fopen(argv[1], "rt") == NULL) { printf(_T("Could not open file %s\n"), argv[1]); return 1; } while (!feof(f)) Add2List(header, GetNode(f)); fclose(f); } else { int PCounter = 2; while (PCounter--) Add2List(header,GetNode(stdin)); } DisplayList(header); return 0; } 

编辑 :从命令提示符运行程序并将文件名作为参数传递