Home > OS >  How do you assign structs into an array?
How do you assign structs into an array?

Time:03-12

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," s %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
...

CodePudding user response:

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, " s %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;
}

CodePudding user response:

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 %%           
  • Related