Here is my text file :

1 SPRITZER 999 2.500000
2 PENCILBOX 999 20.000000
3 IPHONE 90 20.000000
10 GUNNAROPTIK 1919 200.000000

The program let the user enter productID. Listed as 1,2,3,4. And delete the whole line.
I'm currently testing it in main function only. And still fail to do so.
How to do it, please help. Here is my code.

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>

int addProduct();

struct product
{
    int quantity, reorder, i, id;
    char name[20];
    float price;
};

int addProduct()
{
    FILE * fp;

    int i=0;
    struct product a;
    system("cls");

    char checker;

    do
    {
        fp = fopen("addproduct.txt","a+t");
        system("cls");

        printf("Enter product ID : ");
        scanf(" %d", &a.id);

        printf("Enter product name : ");
        scanf(" %s", a.name);

        printf("Enter product quantity : ");
        scanf(" %d", &a.quantity);

        printf("Enter product price : ");
        scanf(" %f", &a.price);

        fprintf(fp, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
        printf("Record saved!\n\n");

        fclose(fp);

        printf("Do you want to enter new product? Y / N : ");

        scanf(" %c", &checker);
        checker = toupper(checker);

        i++;

        system("cls");
    }
    while(checker=='Y');

    if(checker == 'N')
        {
        fp = fopen("addproduct.txt","r");

        while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
            {
        fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price);
        printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
            }

        fclose(fp);

        }

return(0);
}

int searchProduct()
{
FILE * fp;
system("cls");
struct product a;
int productID;

fp = fopen("addproduct.txt","r");
printf("Please enter product ID : ");
scanf(" %d", &productID);

while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
{
    if(a.id == productID)
    {
    printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);

    }
}

fclose(fp);

return(0);
}

int reorderProduct()
{
FILE * fp;
system("cls");
struct product a;
int productQuantity;

fp = fopen("addproduct.txt","r");
printf("Please enter minimum reorder level : ");
scanf(" %d", &productQuantity);

while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
{
    if(a.quantity < productQuantity)
    {
    printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);

    }
}

return (0);
}

int main()
{
    FILE *fp;
        system("cls");
        struct product a;

    int counter=0;

    FILE *ptr2 = fopen("file2.txt","a");
    int productID;

    fflush(stdin);
    printf("Enter product ID you want to edit:\n");
    scanf(" %d",&productID);
    while(a.id!=productID)
    {
        fread(&a,sizeof(struct product),1,fp);
        if(a.id==productID)
        {
        }
        else
        {
            fwrite(&a,sizeof(struct product),1,ptr2);
        }
        counter++;
    }

    remove("addproduct.txt");
    rename("file2.txt","addproduct.txt");
    printf("Press any key..");
    getch();
}

Before commenting too much on the code itself, I want to make sure I understand what the goal is. This is an inventory management program, correct? The file you have listed above shows that you have four different products with names (Spritzer, Pencil, IPhone, GUNNAROPTIK) and corresponding ID (1,2,3,10). I assume that if I am using your program, I am working at a store or warehouse or something similar keeping track of inventory, correct? So if I sell or ship five IPhones, I run your program and enter that information and your program is supposed to subtract 5 from the number of IPhones in order to keep an accurate count? I can use your program to add or subtract from the inventory of one of the four item types or I can add a new item type?

I'm asking these questions because I don't want to give you advice on your program using faulty assumptions.

That said, a few general comments. One, I see something in your code that refers to a file called addproduct.txt. I see that you have a file above your code. However, I see no filename for that file. Is that addproduct.txt? Or is that the inventory file itself? If it's the inventory file, I think you should call it inventory.txt. Naming is important. If I'm NOT adding to the inventory and I want to, for instance just VIEW the inventory, I would not expect to provide your program a file called addproduct.txt. There's also something called file2.txt. Again, not very descriptive. I'm also seeing a lack of comments in your code so it's hard to know what your intent is and how to use your code and your files.

I see what appears to be a big problem as far as reading from the file to the struct. You are using fread and sizeof, which is generally used with binary data to read from the file directly to the struct. However, you are storing your data in the file as white-space delimited text, not binary data. You also have data members called i and reorder in your struct that are not stored in your text file. Mixing binary data/fread/fwrite and white-space delimited text/fscanf is a recipe for disaster.

As for your segmentation fault, I'm seeing a FILE* variable called fp declared on line 125. There is an attempt to read from fp on line 139, but I don't see anywhere where a file is opened and fp is associated with that file, so there's an error right there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.