1

I have currently made this much of the code:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#define STRSIZE 21
struct PInven{
 int count;
 struct PItem{
 char name[STRSIZE];
 int amount;
 }Pitem;
}Pinven;//this needs to be an output file
 int ReadInProduce (){
 //read in file and check to see if the file exist or not.
 FILE * PinFile = fopen("produce.txt","r");
 if (PinFile == NULL){
 printf("ERROR: WRONG FILE");
 }
 else{
 printf("I did it!!\n");
 }
 //assigning the value gotten into the struct variable(but need to maybe change this since it needs to be an output)
 fscanf(PinFile,"%d",&Pinven.count);
 printf("%d\n", Pinven.count);
 int i;
 for(i =0; i <Pinven.count; i++){
 fscanf(PinFile,"%20s %d",Pinven.Pitem.name, &Pinven.Pitem.amount);
 printf("%s %d\n",Pinven.Pitem.name, Pinven.Pitem.amount);
 }
 //making an array to hold the variables
 //FILE * PoutFile = fopen("produce_update.txt","w");
 fclose(PinFile);
 return 0;
}

From there I want to get the file that is read to the structs to be printed out into an array so that later on I can make a function that will be able to compare to the to it.

Basically a store management system. Where the file of the inventory is read in and compared to the file that is store and return a new value for the amount of produce now either left or gained.

10 //number of items that will be stored in the store
apple 19
banana 31
broccoli 9
...
Jabberwocky
51.3k18 gold badges71 silver badges127 bronze badges
asked Mar 11, 2022 at 15:23
3
  • To clarify, do you want to store just the amounts of each item in an array of ints? Or do you want to store the entire struct PItems in an array of struct PItems? Or... something else? Commented Mar 11, 2022 at 15:45
  • 1
    ERROR: WRONG FILE is not a helpful error messages. Error messages should be written to stderr and be informative. eg: const char * path = "produce.txt"; FILE * PinFile = fopen(path, "r"); if (PinFile == NULL){ perror(path);... Commented Mar 11, 2022 at 15:58
  • @melonduofromage so what I want to do is store the count of each produce in the [Pinven.count] and I want to store total value of how many produce I have in the nested struct of '[ Pinven.Pitem.amount ]' (sorry i am new to this and don't know how to comment. i read the thing but i don't understand how to properly highlight it. Commented Mar 11, 2022 at 16:12

2 Answers 2

1

In general, it's a really bad idea to include header information in the file about the number of entries in the file. You want to be able to do stream processing, and that will be more difficult if you need that meta-data. More importantly, it is important to understand how to write the code so that you don't need it. It's not really that difficult, but for some reason people avoid it. One simple approach is just to grow the array for each entry. This is horribly inefficient, but for the sake of simplicity, here's an example that expects the file not not include that first line:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <limits.h>
#define STRSIZE 128
struct PItem{
 char name[STRSIZE];
 int amount;
};
struct PInven{
 int count;
 struct PItem *PItem;
};
static void
grow(struct PInven *p)
{
 p->PItem = realloc(p->PItem, ++p->count * sizeof *p->PItem);
 if( p->PItem == NULL ){
 perror("out of memory");
 exit(1);
 }
}
int
ReadInProduce(struct PInven *P, const char *path)
{
 FILE * PinFile = fopen(path, "r");
 if( PinFile == NULL ){
 perror(path);
 exit(1);
 }
 char fmt[64];
 int max_len;
 max_len = snprintf(fmt, 0, "%d", INT_MAX);
 snprintf(fmt, sizeof fmt, "%%%ds %%%dd", STRSIZE - 1, max_len - 1);
 grow(P);
 struct PItem *i = P->PItem;
 while( fscanf(PinFile, fmt, i->name, &i->amount) == 2 ){
 i += 1;
 grow(P);
 }
 P->count -= 1;
 fclose(PinFile); /* Should check for error here! */
 return P->count;
}
int
main(int argc, char **argv)
{
 struct PInven P = {0};
 char *input = argc > 1 ? argv[1] : "produce.txt";
 ReadInProduce(&P, input);
 struct PItem *t = P.PItem;
 for( int i = 0; i < P.count; i++, t++ ){
 printf("%10d: %s\n", t->amount, t->name);
 }
}

As an exercise for the reader, you should add some error handling. At the moment, this code simply stops reading the input file if there is bad input. Also, it would be a useful exercise to do fewer reallocations.

answered Mar 11, 2022 at 16:30
Sign up to request clarification or add additional context in comments.

1 Comment

your program is missing free P.PItem
0

you should change Structure of PInven to it can save a dynamic array of Pitem with a Pitem pointer.

tested :

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define STRSIZE 21
typedef struct {
 char name[STRSIZE];
 int amount;
} Pitem;
struct PInven {
 int count;
 Pitem *pitem;
} Pinven; // this needs to be an output file
int main() {
 // read in file and check to see if the file exist or not.
 FILE *PinFile = fopen("produce.txt", "r");
 if (PinFile == NULL) {
 printf("ERROR: WRONG FILE");
 } else {
 printf("I did it!!\n");
 }
 // assigning the value gotten into the struct variable(but need to maybe
 // change this since it needs to be an output)
 fscanf(PinFile, "%d", &Pinven.count);
 Pinven.pitem = (Pitem *)malloc(sizeof(Pitem) * Pinven.count);
 printf("%d\n", Pinven.count);
 int i;
 for (i = 0; i < Pinven.count; i++) {
 fscanf(PinFile, "%20s %d", Pinven.pitem[i].name,
 &Pinven.pitem[i].amount);
 // printf("%s %d\n",Pinven.pitem[i].name, Pinven.pitem[i].amount);
 }
 for (i = 0; i < Pinven.count; i++) {
 printf("%s %d\n", Pinven.pitem[i].name, Pinven.pitem[i].amount);
 }
 // making an array to hold the variables
 // FILE * PoutFile = fopen("produce_update.txt","w");
 fclose(PinFile);
 // remember free
 free(Pinven.pitem);
 return 0;
}
answered Mar 11, 2022 at 15:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.