c - Reading text file into structure -
i trying write program can take data text file , put struct, add struct linked list , later display list. no arrays. having problems reading line line each field of struct, when try display list, gives me wrong values/gibberish. section focusing on in main method.
this text file trying read:
#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
and program far:
#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(); char line[50]; int i, j; = 0; file *fp = fopen("input.txt", "r"); if( fp != null ) { while(fgets(line, sizeof(line), fp)) { struct node *nnode = (struct node*)malloc(sizeof node); struct inventory *newnode = (struct inventory*)malloc(sizeof inventory); fscanf(fp,"%s %d %d %f %f ", newnode->invname, &newnode->invpartno,&newnode->invqoh,&newnode->invunitcost,&newnode->invprice); /* fscanf(fp, "%s", newnod->invname); fscanf(fp, "%d", newnod->invpartno); fscanf(fp, "%d", newnod->invqoh); fscanf(fp, "%f", newnod->invunitcost); fscanf(fp, "%f", newnod->invprice); */ nnode->item.dataitem = newnode; nnode->item.nodecounter++; add2list(header, nnode); } } displaylist(header); //startnode->invname = array[0].invname; //struct inventory *startnode; /* char line[bufsiz]; while ( fgets(line, sizeof line, fp) != null && sscanf(line, " %s %d %11s %19s %lf", &record.empnum, record.firstname, record.lastname, &record.hourrate) == 4 ) { }*/ /* struct node *header; header = initlist(); int pcounter = 2; while (pcounter--) { add2list(header,getnode(stdin)); } 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", 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; } struct inventory* readfromfile( ) { /* file *fp; fp = fopen("input.txt", "r"); struct inventory *header, *temp; //header = initlist(); char invname[36]; int invpartno; int invqoh; float invunitcost; float invprice; fscanf(fp, "%s", temp->invname); fscanf(fp, "%d", &temp->invpartno); fscanf(fp, "%d", &temp->invqoh); fscanf(fp, "%f", &temp->invunitcost); fscanf(fp, "%f", &temp->invprice); fclose(fp); */ /* char invname[36]; int invpartno; int invqoh; float invunitcost; float invprice; //--------------------------------------------------------------- char ch, file_name[100]; file *fp; printf("enter name of file wish see\n"); gets(file_name); fp = fopen(file_name,"r"); // read mode if( fp == null ) { perror("error while opening file.\n"); exit(exit_failure); } //--------------------------------------------------------------- int = 0; //while( ( ch = fgetc(fp) ) != eof) //while(!feof(fp)) while((ch != '\n') && (ch != eof)) { struct inventory *temp; char *fgets(temp->invname, 100, fp); fscanf(fp, "%d", &temp->invpartno); fscanf(fp, "%d", &temp->invqoh); fscanf(fp, "%f", &temp->invunitcost); fscanf(fp, "%f", &temp->invprice); }*/ return 0; } void readfile() { stock array[20]; //9 items read list int i, j; = 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++; } } }
you read line fgets
, try read struct fscanf
. cannot work because fgets
eats line, missing when fscanf
. code might
struct inventory buf; while (fscanf(fp,"%s %d %d %f %f ", buf.invname, &buf.invpartno, ...) == 5) { /* data, copy dynamic memory */ struct node *nnode = (struct node*)malloc(sizeof node); struct inventory *newnode = (struct inventory*)malloc(sizeof inventory); *newnode = buf; nnode->item.dataitem = newnode; nnode->item.nodecounter++; add2list(header, nnode); }
this reads input temporary buffer, can used later, when storing or processing input.
update:
another approach test eof , leave rest of loop
while(!feof(fp)) { struct node *nnode = (struct node*)malloc(sizeof node); struct inventory *newnode = (struct inventory*)malloc(sizeof inventory); 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); }
Comments
Post a Comment