-
Notifications
You must be signed in to change notification settings - Fork 16
/
10.c
68 lines (64 loc) · 1.39 KB
/
10.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
struct Product
{
int code;
float cost;
int quantity;
};
int append_data()
{
FILE *fp;
fp = fopen("products.txt", "a");
struct Product products;
printf("\nEnter product code: ");
scanf("%d", &products.code);
printf("Enter product cost: ");
scanf("%f", &products.cost);
printf("Enter product quantity: ");
scanf("%d", &products.quantity);
fprintf(fp, "%d %f %d\n", products.code, products.cost, products.quantity);
fclose(fp);
return 0;
}
int filter_with_code(int code)
{
FILE *fp;
fp = fopen("products.txt", "r");
struct Product products[5];
int i = 0;
while (!feof(fp))
{
fscanf(fp, "%d %f %d", &products[i].code, &products[i].cost, &products[i].quantity);
if (products[i].code == code)
{
printf("%d %f %d\n", products[i].code, products[i].cost, products[i].quantity);
}
i++;
}
fclose(fp);
return 0;
}
int main()
{
printf("1. Append data\n");
printf("2. Filter with code\n");
printf("Enter choice: ");
int choice;
scanf("%d", &choice);
switch (choice)
{
case 1:
append_data();
break;
case 2:
printf("Enter code: ");
int code;
scanf("%d", &code);
filter_with_code(code);
break;
default:
printf("Invalid choice\n");
break;
}
return 0;
}