-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57b87cc
commit 896c1c7
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <stdio.h> | ||
|
||
struct Product | ||
{ | ||
int code; | ||
float cost; | ||
int quantity; | ||
}; | ||
|
||
int main() | ||
{ | ||
FILE *fp; | ||
fp = fopen("products.txt", "w"); | ||
struct Product products[5]; | ||
for (int i = 0; i < 5; i++) | ||
{ | ||
printf("\nEnter product code: "); | ||
scanf("%d", &products[i].code); | ||
printf("Enter product cost: "); | ||
scanf("%f", &products[i].cost); | ||
printf("Enter product quantity: "); | ||
scanf("%d", &products[i].quantity); | ||
fprintf(fp, "%d %f %d\n", products[i].code, products[i].cost, products[i].quantity); | ||
} | ||
fclose(fp); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <stdio.h> | ||
|
||
int main() | ||
{ | ||
FILE *fp; | ||
fp = fopen("products.txt", "r"); | ||
float sum = 0; | ||
while (feof(fp) == 0) | ||
{ | ||
float temp; | ||
fscanf(fp, "%*d %f %*d ", &temp); | ||
sum += temp; | ||
} | ||
fclose(fp); | ||
printf("Total cost: %f\n", sum); | ||
return 0; | ||
} |