Search code examples
cinventory

How to get things in txt and store in array in c


I am a noob in programming, so don't blame me if the code below isn't good. I am doing a project about an inventory system. there is five function in this system including modify and search. My thought is to let user input record number and gets the data stored in txt back to the array and display the one with correct ID.With this, user can edit the thing is the certain structure array. But I can't figure out how to get data in txt to an array. it is poissibe or any alternative solution.

declaraton

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #define true 1
 #define false 0
struct RecordData {

     char RecordNum[10]; 
     char ItemName[ 50 ]; 
     int ItemNum ; 
     char Category[ 50 ];
     int Quantity;
     char Recipient[ 50 ];
     char Destination[ 50 ];
     char Delivery[ 100 ];

}; 
struct RecordData record[MAX];
FILE *fp;

Function add record

int addrecord(){

    int x, i = 0;
    char ch, yesno;
    char space = ' ';

    doagain:

        printf("1) Enter Record Number:");
        gets(record[i].RecordNum);

        printf("2) ItemName\nEnter :");
        gets(record[i].ItemName);

        printf("3) ItemNumber\nEnter :");
        scanf("%d%c", &record[i].ItemNum, &ch);

        printf("4) Category\nEnter : ");
        gets(record[i].Category);

        printf("5) Quantity\nEnter : ");
        scanf("%d%c", &record[i].Quantity, &ch);

        printf("7) Recipient\nEnter : ");
        gets(record[i].Recipient);

        printf("8) Final Destination\nEnter : ");
        gets(record[i].Destination);

        printf("9) Delivery status \nEnter : ");
        gets(record[i].Delivery);

        fp = fopen("stock.txt", "a");

        fprintf(fp, "%04d\n", i);

        fprintf(fp, "%s\n%d\n%s\n%d\n%s\n%s\n%s\n%c\n", record[i].ItemName, record[i].ItemNum, 
        record[i].Category, record[i].Quantity, record[i].Recipient, record[i].Destination, record[i].Delivery, space);
        fclose(fp);

    enterys:

        printf("Do you want to add other record? Yes(Y) or No(N)");
        scanf("%s", &yesno);

    switch (yesno){

        case 'Y':
        case 'y':
            i++;
            goto doagain;
            break;
        case 'N':
        case 'n':
            printf("end program\n");
            system("cls");
            return main();
            break;
        default:
            printf("you have enter wrong input");
            goto enterys;

    }

}

main

int main(){

    int num;
    char space, ch;
    mainGUI:
        printf("1. Add New Item<s>:");
        printf("\n2. Display Item Record:");
        printf("\n3. Search Item Information:");
        printf("\n4. Modify Item Record<s>:");
        printf("\nDelete Item Record<s>:");
        printf("\nWhat is your option? <1-5>");
        scanf("%d", &num);
        system("cls");

        switch (num)
        {
            case 0:
                printf("Quit^_^");
                system("cls");
                break;
            case 1:
                printf("You Are Now Adding New Item<s>:\n");
                addrecord();
                break;
            case 2:

                printf("You Are Now Displaying Item<s>:");
                display();
                printf("Press any Button to Go Back Menu");
                scanf("%c%c", &ch, &space);
                if (space == ' '){

                    system("cls");  
                    goto mainGUI;   

                }else{

                    system("cls");
                    goto mainGUI;

                }

                break;
            case 3:
                printf("You Are Now searching items New Item<s>:");
                break;
            case 4:
                printf("You Are Now Adding New Item<s>:");
                break;
            case 5:
                printf("You Are Now Adding New Item<s>:");
                break;
            default:
            printf("Enter Wrong input\n");
            goto mainGUI;
            break;




        }
    return 0;


}

Solution

  • Checkout below mentioned fragment. This should ring some bells.

    fp = fopen("stock.txt", "r");
    if (fp == NULL){
      printf("Cannot open file \n");
      return 0;
    }
    
    i = 0;
    ch[i] = fgetc(fp);
    while (ch != EOF){
      i++;
      ch[i] = fgetc(fp);
    }
    fclose(fp);
    return 1;