Everything is working fine in my program with no compliling errors. However, when I go to list the data, it writes it twice. I can't figure out what is wrong in my code.

#include <stdio.h>
#include <string.h>


struct inventory
{
    int number;
    char toolName[15];
    int quantity;
    double price;
};

int enterChoice(void);
void List( FILE *readPtr);
void updateRecord( FILE *fPtr);
void newRecord (FILE *fPtr);
void deleteRecord (FILE *fPtr);

int main (void)
{
    FILE *CtPtr;
    int choice;

    if ( ( CtPtr = fopen( "hardware.dat", "rb+" ) ) == NULL ) {
        if ( ( CtPtr = fopen( "hardware.dat", "wb" ) ) == NULL );
    }
    while (( choice = enterChoice())!= 4){
        switch (choice){
            case 1:
                newRecord(CtPtr);
                break;
            case 2 :
                deleteRecord(CtPtr);
                break;
            case 3:
                List(CtPtr);
                break;
            default:
                printf("Incorrect choice\n");
                break;
        }
    }
    fclose (CtPtr);
    return 0;
}


int enterChoice (void)
{
    int menuChoice;

    printf("1 - Input new tool or update an existing tool\n"
           "2 - Delete a tool\n"
           "3 - List all tools\n"
           "4 - Exit\n");
    scanf ("%d", &menuChoice);
    return menuChoice;
}

void newRecord (FILE*fPtr)
{
    struct inventory data={0,"",0,0.0};

    int toolNumber;

    printf ("Enter record number (1-100):");
    scanf ("%d", &toolNumber);

    fseek( fPtr, (toolNumber-1)*sizeof(struct inventory),SEEK_SET);
    fread(&data, sizeof(struct inventory),1,fPtr);


    if (data.number !=0){
        printf( "%-12d%-13s%-10d%-10.2f\n\n", data.number, data.toolName, data.quantity, data.price );

        int index = 0;
        printf( "Enter new tool name: ");
        fgets( data.toolName, 20, stdin );
        index = strlen( data.toolName)-1;
        if( data.toolName[ index ] == '\n' )
            data.toolName[index]='\0';
        fscanf(stdin, "%s", data.toolName);
        printf( "Enter new quantity: ");
        fscanf( stdin, "%d", &data.quantity );
        printf( "Enter new price: ");
        fscanf( stdin, "%lf", &data.price );

        printf( "%-12s%-13s%-10s%-10s\n%-12d%-13s%-10d%-10.2f\n\n", "Record #", "Tool Name", "Quantity", "Cost", data.number, data.toolName, data.quantity, data.price );

        int record;
        fseek( fPtr, ( record - 1 ) * sizeof( struct inventory ), SEEK_SET );

        fwrite( &data, sizeof( struct inventory ), 1, fPtr );

    }
    else{
        printf("Enter Tool Name, quantity, and price\n?");
        scanf("%s%d%lf", data.toolName, &data.quantity, &data.price);

        data.number = toolNumber;

        fseek(fPtr, (data.number - 1)*sizeof(struct inventory), SEEK_SET);

        fwrite(&data, sizeof(struct inventory), 1,fPtr);

    }
}



void deleteRecord (FILE *fPtr)
{
    struct inventory data;
    struct inventory blankRecord = {0,"",0,0};

    int toolNumber;

    printf("Enter record number to delete (1-100):");
    scanf("%d", &toolNumber);

    fseek( fPtr, (toolNumber-1)*sizeof(struct inventory), SEEK_SET);

    fread ( &data, sizeof(struct inventory),1,fPtr);

    if(data.number==0){
        printf("Record %d does not exist.\n", toolNumber);
    }
    else{
        fseek(fPtr, (toolNumber-1)*sizeof(struct inventory), SEEK_SET);
        fwrite(&blankRecord, sizeof(struct inventory), 1, fPtr);
    }
}

void List(FILE *readPtr)
{

    struct inventory data = {0,"",0,0.00};

    rewind(readPtr);
    printf("%-12s%-13s%-10s%-10s\n", "Record#", "Tool Name", "Quantity", "Price\n");

    while (!feof(readPtr)){
        fread( &data, sizeof(struct inventory), 1, readPtr);

        if (data.number !=0){
            printf("%-12d%-13s%-10d%-10.2f\n\n", data.number, data.toolName, data.quantity, data.price );

        }
    }
}

Recommended Answers

All 2 Replies

First, this is not good:

if ( ( CtPtr = fopen( "hardware.dat", "rb+" ) ) == NULL ) {
    if ( ( CtPtr = fopen( "hardware.dat", "wb" ) ) == NULL );
}

In the second condition, it just falls through even if the file cannot be opened for write access (create).

Please show sample output that illustrates your problem with duplicates. I am not seeing anything in the code (on cursory inspection) that would cause that.

1 - Input new tool or update an existing tool
2 - Delete a tool
3 - List all tools
4 - Exit
1
Enter record number (1-100):1
Enter Tool Name, quantity, and price
?Hammer 2 20
1 - Input new tool or update an existing tool
2 - Delete a tool
3 - List all tools
4 - Exit
3
Record#     Tool Name    Quantity  Price

1           Hammer       2         20.00     

1 - Input new tool or update an existing tool
2 - Delete a tool
3 - List all tools
4 - Exit
1
Enter record number (1-100):2
Enter Tool Name, quantity, and price
?Wrench 3 15
1 - Input new tool or update an existing tool
2 - Delete a tool
3 - List all tools
4 - Exit
3
Record#     Tool Name    Quantity  Price

1           Hammer       2         20.00     

2           Wrench       3         15.00     

2           Wrench       3         15.00     

1 - Input new tool or update an existing tool
2 - Delete a tool
3 - List all tools
4 - Exit
1
Enter record number (1-100):3
Enter Tool Name, quantity, and price
?Pliers 1 12
1 - Input new tool or update an existing tool
2 - Delete a tool
3 - List all tools
4 - Exit
3
Record#     Tool Name    Quantity  Price

1           Hammer       2         20.00     

2           Wrench       3         15.00     

3           Pliers       1         12.00     

3           Pliers       1         12.00     

1 - Input new tool or update an existing tool
2 - Delete a tool
3 - List all tools
4 - Exit
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.