Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Author: vsavovpc
- #include <stdio.h>
- #include <stdlib.h>
- struct student {
- int id;
- char name[50];
- char spec[50];
- double price;
- };
- int main()
- {
- int input = 0;
- while (scanf("%d",&input))
- {
- if (input == 3)
- {
- break;
- }
- switch (input)
- {
- case 1: {
- FILE *fp;
- int i;
- struct student n;
- if ((fp = fopen("gosho", "wb")) == NULL) {
- exit(1);
- }
- for (i = 0; i < 1; i++)
- {
- printf("id: ");
- fflush(stdin);
- scanf("%d", &n.id);
- printf("Name: ");
- fflush(stdin);
- scanf("%s", &n.name);
- printf("Spec: ");
- fflush(stdin);
- scanf("%s", &n.spec);
- printf("price: ");
- fflush(stdin);
- scanf("%lf", &n.price);
- fwrite(&n, sizeof(n), 1,fp);
- }
- fclose(fp);
- }; break;
- case 2:{
- FILE *fp;
- fp = fopen("gosho", "rb");
- struct student n;
- int id;
- char name[50];
- char spec[50];
- double price = 100000;
- while (fread(&n, 1, sizeof(n), fp) != NULL){
- printf("print from reading: id: %d name: %s spec: %s price: %lf\n", n.id ,n.name, n.spec, n.price);
- if (price > n.price)
- {
- id = n.id;
- strncpy(name, n.name, 50);
- strncpy(spec, n.spec, 50);
- price = n.price;
- }
- }
- printf("Product with min price is: id: %d name: %s spec: %s price: %lf\n", id, name, spec, price);
- fclose(fp);
- }; break;
- default:
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment