Hey all...this is my grocery inventory program, also I aware to not use while (!feof(fp) however I haven't yet sorted through my code to find the correct statement. Also, i am wondering why my temp file is not being created (tmpfile, temp.txt). Thanks for help.
Code:
...
...
void remove_entry(void)
{
FILE *fp;
FILE *fp2;
FILE *fp3;
FILE *tmpfile;
char str[MAXCHAR];
char str2[MAX];
fp = fopen("REFRIGERATOR.TXT", "r+");
fp2 = fopen("FREEZER.TXT", "r+");
fp3 = fopen("DRY_STOCK.TXT", "r+");
tmpfile = fopen("temp.txt", "w"); // open temporary file in write mode
int ln, number, ctr;
ctr = 0;
if (!tmpfile)
{
printf("Unable to open a temporary file to write!\n");
exit(0);
}
puts("\n1. for Fridge\n2. for Freezer\n3. for dry shelve\n");
scanf("%d", &number);
if(number == 1)
{
// Print REFRIGERATOR.TXT to screen
while(fgets(str, MAXCHAR, fp) != NULL)
{
printf("%s", str);
}
puts("\n");
printf("Input the line you want to remove: ");
scanf("%d", &ln);
ln++;
// copy all contents to the temporary file except the specific line
while (!feof(fp))
{
strcpy(str2, "\0");
fgets(str2, MAX, fp);
if (!feof(fp))
{
ctr++;
/* skip the line at given line number */
if (ctr != ln)
{
fprintf(tmpfile, "%s", str2);
}
}
}
menu();
fclose (fp);
fclose (tmpfile);
}
}