Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am writing a program in C to take user input, and modify different aspects of

ID: 3574890 • Letter: I

Question

I am writing a program in C to take user input, and modify different aspects of an inventory list. However, I need the file "records" to be an external .txt file, that can be viewed separately to confirm that it was written, anybody know what to do? I attempted using the full file path and got my own error message "file empty". I am at my wits end. The code is below.

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


/**DEFINE**/
struct INVENTORY
{
int SKU;
char ALBUM[200];
float PRICE;
}INV;


/**FUNCTION TO INSERT RECORDS TO THE FILE**/
void insert()
{
FILE *fp;
fp = fopen("records", "a");
printf("Enter the SKU of the ALBUM(last 4 numbers :");
scanf("%d", &INV.SKU);
printf("Enter the ALBUM :");
scanf("%s", &INV.ALBUM);
printf("Enter the PRICE :");
scanf("%f", &INV.PRICE);
fwrite(&INV, sizeof(INV), 1, fp);
fclose(fp);
}


/**FUNCTION TO DISPLAY RECORDS**/
void disp()
{
FILE *fp1;
fp1 = fopen("records", "r");
printf(" SKU ALBUM PRICE ");
while (fread(&INV, sizeof(INV), 1, fp1))
printf(" %d %s %.2f ", INV.SKU, INV.ALBUM, INV.PRICE);
fclose(fp1);
}


/**FUNCTION TO SEARCH FOR AN ITEM BY ITS SKU**/
void search()
{
FILE *fp2;
/**DECLARING VARIABLES**/
int r, s, avl;
/**ASK FOR NUMERICAL USER INPUT, AND START IF STATEMENT IF DATA
DOES NOT EXIST, THEN DISPLAY ERROR MESASGE**/
printf(" Enter the SKU you want to search :");
scanf("%d", &r);
avl = avlSKU(r);
if (avl == 0)
printf("SKU %d is not available in the file ",r);
/**IF SKU DATA IS AVAILABLE FROM THE RECORDS FILE, READ THE
INV STRUCTURE AND DISPLAY ITS COMPONENENTS**/
else
{
fp2 = fopen("records", "r");
while (fread(&INV, sizeof(INV), 1, fp2))
{
s = INV.SKU;
if (s == r)
{
printf(" SKU = %d", INV.SKU);
printf(" ALBUM = %s", INV.ALBUM);
printf(" PRICE = %.2f ", INV.PRICE);
}
}
fclose(fp2);
}
}


/**FUNCTION TO DELETE AN INVENTORY ITEM**/
void deletefile()
{
FILE *fpo;
FILE *fpt;
int r, s;
printf("Enter the SKU you want to delete :");
/**RECIEVE USERS SKU SELECTION AND DETERMINE IF
IT EXISTS WITHIN THE FILE, IF NOT DISPLAY ERROR**/
scanf("%d", &r);
if (avlSKU(r) == 0)
printf("SKU %d is not available in the file ", r);

else
{
fpo = fopen("records", "r");
fpt = fopen("TempFile", "w");
/** AS THE PROGRAM READS THE INV
STRUCTURE, REWRITE THE CHOSEN COMPONENTS OF
THE STRUCTURE BACK TO BLANK, ESSENTIALLY REMOVING
THEM ALLTOGETHER**/
while (fread(&INV, sizeof(INV), 1, fpo))
{
s = INV.SKU;
if (s != r)
fwrite(&INV, sizeof(INV), 1, fpt);
}
fclose(fpo);
fclose(fpt);
fpo = fopen("records", "w");
fpt = fopen("TempFile", "r");
while (fread(&INV, sizeof(INV), 1, fpt))
fwrite(&INV, sizeof(INV), 1, fpo);
printf(" RECORD DELETED ");
fclose(fpo);
fclose(fpt);
}

}

/**FUNCTION TO UPDATE THE RECORD**/
void update()
{
int avl;
FILE *fpt;
FILE *fpo;
int s, r, ch;
printf("Enter SKU to update:");
scanf("%d", &r);
/**MY FRIEND HELPED HERE, USING A BINARY TREE, WE
ARE ABLE TO SEE IF WE CAN RETRIEVE SKU, AND THEN
USE 0 OR 1 AS THE DETERMINING OPERAND FOR OUR
IF/ELSE STATEMENT**/
avl = avlSKU(r);
if (avl == 0)
{
printf("SKU %d is not Available in the file", r);
}
else
{
fpo = fopen("records", "r");
fpt = fopen("TempFile", "w");
while (fread(&INV, sizeof(INV), 1, fpo))
/**HERE, WE CREATE A TEMPORARY VALUE,
STORED AS FPT, THEN WE WRITE THAT VALUE
TO ITS POSITION WITHIN THE INV STRUCTURE,
WE CHOOSE WHAT VALUE WITH OUR SWITCH
AND CASE STATEMENTS**/
{
s = INV.SKU;
if (s != r)
fwrite(&INV, sizeof(INV), 1, fpt);
else
{
printf(" 1. Update ALBUM of SKU %d", r);
printf(" 2. Update PRICE of SKU %d", r);
printf(" 3. Update both ALBUM and PRICE of SKU %d", r);
printf(" Enter your choice:");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter ALBUM:");
scanf("%s", &INV.ALBUM);
break;
case 2:
printf("Enter PRICE : ");
scanf("%f", &INV.PRICE);
break;
case 3:
printf("Enter ALBUM: ");
scanf("%s", &INV.ALBUM);
printf("Enter PRICE: ");
scanf("%f", &INV.PRICE);
break;
default:
printf("Invalid Selection");
break;
}
fwrite(&INV, sizeof(INV), 1, fpt);
}
}
fclose(fpo);
fclose(fpt);
fpo = fopen("records", "w");
fpt = fopen("TempFile", "r");
while (fread(&INV, sizeof(INV), 1, fpt))
{
fwrite(&INV, sizeof(INV), 1, fpo);
}
fclose(fpo);
fclose(fpt);
printf("RECORD UPDATED");
}
}

/** FUNCTION TO SORT THE RECORD **/
void sort()
{
int a[20], count = 0, i, j, t, c;
FILE *fpo;
fpo = fopen("records", "r");
while (fread(&INV, sizeof(INV), 1, fpo))
{
a[count] = INV.SKU;
count++;
}
c = count;
for (i = 0; i<count - 1; i++)
{
for (j = i + 1; j<count; j++)
{
if (a[i]>a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
printf("SKU. ALBUM PRICE ");
count = c;
for (i = 0; i<count; i++)
{
rewind(fpo);
while (fread(&INV, sizeof(INV), 1, fpo))
{
if (a[i] == INV.SKU)
printf(" %d %s %2f",INV.SKU, INV.ALBUM, INV.PRICE);
}

}
}

/** FUNCTION TO CHECK GIVEN SKU IS AVAILABLE*/
int avlSKU(int rno)
{
FILE *fp;
int c = 0;
fp = fopen("records", "r");
while (!feof(fp))
{
fread(&INV, sizeof(INV), 1, fp);

if (rno == INV.SKU)
{
fclose(fp);
return 1;
}
}
fclose(fp);
return 0;
}


/**FUNCTION TO CHECK THE FILE IS EMPTY OR NOT**/
int empty()
{
int c = 0;
FILE *fp;
fp = fopen("records", "r");
while (fread(&INV, sizeof(INV), 1, fp))
c = 1;
fclose(fp);
return c;
}

/**MAIN PROGRAM**/
void main()
{
int c, emp;
do
{
printf(" ---Select your choice--------- ");
printf(" 1. INSERT 2. DISPLAY 3. SEARCH");
printf(" 4. DELETE 5. UPDATE 6. SORT BY SKU");
printf(" 7. EXIT");
printf(" ------------------------------------------ ");
printf(" Enter your choice:");
scanf("%d", &c);
printf(" ");
switch (c)
{
case 1:
insert();
break;
case 2:
emp = empty();
if (emp == 0)
printf(" The file is EMPTY ");
else
disp();
break;
case 3:
search();
break;
case 4:
deletefile();
break;
case 5:
update();
break;
case 6:
emp = empty();
if (emp == 0)
printf(" The file is EMPTY ");
else
sort();
break;
case 7:
exit(1);
break;
default:
printf(" You have made an invalid selection! Please try again... ");
break;

}
} while (c != 7);


}

Explanation / Answer

Either place the file in the directory where the program is there and compile and run the program from the same directory. Give the name of the file with extension in the fopen command. ( fp = fopen("records.txt", "a");)

OR , place the file in any directory and give the full path of the file in the fopen command.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote