veselinsavov

Ivo

Mar 11th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void Read(FILE *, char);
  4.  
  5. int main()
  6. {
  7.     FILE *fp = NULL;
  8.     int size = 0;
  9.     int id = 0;
  10.     int i;
  11.     char fname[50];
  12.     char name[50];
  13.     char spec[50];
  14.     double price = 0;
  15.     printf("File name: ");
  16.     //fgets(fname, 49, stdin);
  17.     scanf("%s", &fname);
  18.     fp = fopen(fname, "wb");
  19.     if (fp == NULL)
  20.     {
  21.         printf("Error1\n");
  22.         return 0;
  23.     }
  24.     else
  25.     {
  26.         for (i = 0; i < 2; i++)
  27.         {
  28.             printf("ID: ");
  29.             fflush(stdin);
  30.             scanf("%d", &id);
  31.             if (fwrite(&id, sizeof(int), 1, fp) != 1)
  32.             {
  33.                 printf("%d", id);
  34.                 //return 0;
  35.             }
  36.             printf("Name: ");
  37.             fflush(stdin);
  38.             scanf("%s", &name);
  39.             size = sizeof(name);
  40.             if (fwrite(&size, sizeof(int), 1, fp) != 1)
  41.             {
  42.                 printf("Error2\n");
  43.                 return 0;
  44.             }
  45.             if (fwrite(name, 1, strlen(name), fp) != strlen(name))
  46.             {
  47.                 printf("Error3\n");
  48.                 return 0;
  49.             }
  50.             printf("Spec: ");
  51.             fflush(stdin);
  52.             scanf("%s", &spec);
  53.             size = sizeof(spec);
  54.             if (fwrite(&size, sizeof(int), 1, fp) != 1)
  55.             {
  56.                 printf("Error3\n");
  57.                 return 0;
  58.             }
  59.             if (fwrite(spec, 1, strlen(spec), fp) != strlen(spec))
  60.             {
  61.                 printf("Error4\n");
  62.                 return 0;
  63.             }
  64.             printf("Price: ");
  65.             fflush(stdin);
  66.             scanf("%1f", &price);
  67.             if (fwrite(&price, sizeof(double), 1, fp) != 1)
  68.             {
  69.                 printf("Error5\n");
  70.                 return 0;
  71.             }
  72.         }
  73.         fclose(fp);
  74.     }
  75.     Read(fp, fname);
  76. }
  77.  
  78. void Read(FILE *fp, char fname[50])
  79. {
  80.     int i;
  81.     int id = 0;
  82.     int size = 0;
  83.     char name[50];
  84.     char spec[50];
  85.     double price = 0;
  86.     fp = fopen(fname, "rb");
  87.     if (fp == NULL)
  88.     {
  89.         printf("error\n");
  90.         return 0;
  91.     }
  92.     else
  93.     {
  94.         for (i = 0; i < 2; i++)
  95.         {
  96.             if (fread(&id, sizeof(int), 1, fp) != 1)
  97.             {
  98.                 printf("error\n", id);
  99.                 return 0;
  100.             }
  101.             printf("ID: %d\n", id);
  102.             if (fread(&size, sizeof(int), 1, fp) != 1)
  103.             {
  104.                 printf("Error2\n");
  105.                 return 0;
  106.             }
  107.            
  108.             if (fread(name, 1, size, fp) != size)
  109.             {
  110.  
  111.                 return 0;
  112.             }
  113.             printf("Size = %d\n", size);
  114.             printf("Name: %s\n", name);
  115.             if (fread(&size, sizeof(int), 1, fp) != 1)
  116.             {
  117.                 printf("Error33\n");
  118.                 return 0;
  119.             }
  120.             if (fwrite(spec, 1, size, fp) != size)
  121.             {
  122.                 printf("Error4\n");
  123.                 return 0;
  124.             }
  125.             printf("Spec: %s\n", spec);
  126.             if (fread(&price, sizeof(double), 1, fp) != 1)
  127.             {
  128.                 printf("Error5\n");
  129.                 return 0;
  130.             }
  131.             printf("Price = %g\n", price);
  132.         }
  133.         fclose(fp);
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment