Write a program in C that read a file name billtxt and write

Write a program in C that read a file name bill.txt and write a new file namedtaxedbill.txt where the price of every product has added a 6% tax.File has one item per line using the format <codebar>TAB<name>TAB<price>You have to implement and use the following structure and function for reading aproduct from the file:

struct Item {

   int codebar;

  char name[80];

  double price;

}

;int readItem(FILE *fp, struct Item *item)

;/* This function read one item from fp.

   It returns 1 when the item has been read or 0 if EOF has got it

*/

Execution Example:
File bill.txt
1 HP24-g020 4992 CyberPowerPC-GUA520 4933 ASUS-VivoPC 799calling exercise3();

After Execution
File taxedbill.txt
1 HP24-g020 528.9400002 CyberPowerPC-GUA520 522.5800003 ASUS-VivoPC 846.940000

Solution

main.c:

#include <stdio.h>

// declaring the structure for an item
typedef struct Item {   
int codebar;
char name[80];
double price;
} Item;

// read one item at a time from file
// returns -1 in case no item is found now
int readItem(FILE *fp, struct Item *itemNode) {
int codebar;
char name[80];
double price;
  
// read codebar, name and price seperated by tab(\\t)
if(fscanf(fp, \"%d\\t%s\\t%lf\", &codebar, name, &price) != EOF) {
// copy values in item node
itemNode->codebar = codebar;
strcpy(itemNode->name, name);
itemNode->price = price;
  
return 1;
} else {
// if no item is found
return -1;
}
}

// driver program
int main()
{
// input and output file pointer
FILE *inptr;
FILE *outptr;
inptr = fopen(\"C:\\\\Users\\\\ykgupta\\\\Documents\\\\df\\\\d\\\\bill.txt\",\"r\");
outptr = fopen(\"C:\\\\Users\\\\ykgupta\\\\Documents\\\\df\\\\d\\\ amedtaxedbill.txt\",\"w\");
  
// in case if there is some issue in opening files
if((inptr == NULL) || (outptr == NULL))
{
printf(\"Error!\");
exit(1);   
}

// create a temporary item node
Item * itemNode = (Item *) malloc(sizeof(Item));

// keep looping till no item is present
while(1) {
// read item and check status
int status = readItem(inptr, itemNode);
if(status == -1) {
break;
} else {
// write on the output file, based on the input values in itemNode
// itemNode->price * 1.06 is doing the 6% increment in price for tax
fprintf(outptr,\"%d %s %f\ \", itemNode->codebar, itemNode->name, itemNode->price * 1.06);
}
}

// free the memory allocated for item node
free(itemNode);

printf(\"Program completed Successfully\");

// close the file resources
fclose(inptr);
fclose(outptr);

return 0;
}



bill.txt:(Input)
1 HP24-g020 499
2 CyberPowerPC-GUA520 493
3 ASUS-VivoPC 799


namedtaxedbill.txt:(Output file)
1 HP24-g020 528.940000
2 CyberPowerPC-GUA520 522.580000
3 ASUS-VivoPC 846.940000


I have created the above program for your requirement. The file paths are local to my computer. Please change them as per your requirement. Also, Please pay close attention to the input file, where data is seperated by tabs not spaces.

Thanks

Write a program in C that read a file name bill.txt and write a new file namedtaxedbill.txt where the price of every product has added a 6% tax.File has one ite
Write a program in C that read a file name bill.txt and write a new file namedtaxedbill.txt where the price of every product has added a 6% tax.File has one ite
Write a program in C that read a file name bill.txt and write a new file namedtaxedbill.txt where the price of every product has added a 6% tax.File has one ite

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site