thieumao

Bai mau doc ghi file

Jun 22nd, 2014
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define fio "Products.txt"
  5. #define fbin "Products.dat"
  6.  
  7. typedef struct products{
  8.     char Id[50], name[50];
  9.     int quantity;   // so luong
  10.     double cost;    // gia
  11. } product;
  12.  
  13. void input(product prd[], int *n);
  14. void writeBin(product prd[], int n);
  15. void write(product prd[], int n);
  16.  
  17. void sort_cost(product prd[], int n);
  18.  
  19. void readBin(product prd[], int *n);
  20. void output(product prd[], int n);
  21.  
  22. void search_id(product prd[], int n, char id[]);
  23. void search_name(product prd[], int n, char name[]);
  24. void search(product prd[], int n);
  25.  
  26.  
  27. int main() {
  28.     product prd[50];
  29.     int n, i;
  30.     FILE *f = fopen(fio, "r");
  31.  
  32.     int select;
  33.     do {
  34.         printf("Products:\n-----------------\n");
  35.         printf("1: input\n2: read and sort by cost\n3: Search\n4: exit\n");
  36.         printf("Enter the number to work: ");
  37.         scanf("%d", &select);
  38.         switch(select){
  39.             case 1: {
  40.                 printf("1: INPUT\n");
  41.                 input(prd, &n);
  42.                 write(prd, n);
  43.                 break;
  44.             }
  45.             case 2: {
  46.                 printf("2: READ AND SORT BY COST\n");
  47.                 printf("Befor sort:\n--------------\n");
  48.                 readBin(prd, &n);
  49.                 output(prd, n);
  50.                 sort_cost(prd, n);
  51.                 write(prd, n);
  52.                 printf("After sort:\n--------------\n");
  53.                 readBin(prd, &n);
  54.                 output(prd, n);
  55.                 break;
  56.             }
  57.             case 3: {
  58.                 printf("SEARCH\n");
  59.                 search(prd, n);
  60.                 break;
  61.             }
  62.             case 4: return 0;
  63.             default : printf("Error select !"); break;
  64.         }
  65.     }
  66.     while (select != 4);
  67.     return 0;
  68. }
  69.  
  70. void input(product prd[], int *n){
  71.     int i;
  72.     char s[50];
  73.     printf("Enter the number of products: ");
  74.     scanf("%d", n);
  75.     gets(s);
  76.     printf("\n");
  77.     for (i = 0; i < (*n); i++){
  78.         printf("Enter the Id of products %d : ", i+1);
  79.         gets(prd[i].Id);
  80.         printf("\tEnter the nam of products %d : ", i+1);
  81.         gets(prd[i].name);
  82.         printf("\tEnter the quantity of products %d : ", i+1);
  83.         scanf("%d", &prd[i].quantity);
  84.         printf("\tEnter the cost of products %d : ", i+1);
  85.         scanf("%lf", &prd[i].cost);
  86.         gets(s);
  87.     }
  88. }
  89.  
  90. void writeBin(product prd[], int n){
  91.     FILE *f = fopen(fbin,"wb");;
  92.     int i;
  93.     if(f==NULL) printf("Error load file");
  94.     else fwrite(prd,sizeof(product),n,f);
  95.     fclose(f);
  96. }
  97.  
  98. void write(product prd[], int n){
  99.     int i;
  100.     FILE *f = fopen(fio, "w");
  101.     if(f==NULL) printf("Error load file");
  102.     fprintf(f, "%-10s %-15s %-10s %-10s\n", "Id", "Name", "Quantity", "Cost");
  103.     for (i = 0; i < n; i++)
  104.         fprintf(f, "%-10s %-15s %-10d %-10.2lf\n", prd[i].Id, prd[i].name, prd[i].quantity, prd[i].cost);
  105.     fclose(f);
  106.     writeBin(prd, n);
  107.     printf("input and write success to file!\n");
  108. }
  109.  
  110. void sort_cost(product prd[], int n){
  111.     int i, j;
  112.     for (i = 0; i < n - 1; i++){
  113.         for (j = i + 1; j < n; j++){
  114.             if (prd[i].cost > prd[j].cost){
  115.                 product temp = prd[i];
  116.                 prd[i] = prd[j];
  117.                 prd[j] = temp;
  118.             }
  119.         }
  120.     }
  121. }
  122.  
  123. void output(product prd[], int n){
  124.     int i;
  125.     printf("%-10s %-10s %-15s %-10s %-10s\n", "Order", "Id", "Name", "Quantity", "Cost");
  126.     for (i = 0; i < n; i++)
  127.         printf("%-10d %-10s %-15s %-10d %-10.2lf\n", i + 1, prd[i].Id, prd[i].name, prd[i].quantity, prd[i].cost);
  128. }
  129.  
  130.  
  131.  
  132. void readBin(product prd[], int *n){
  133.     FILE *f = fopen(fbin,"rb");
  134.     fseek(f,0,SEEK_END); //Nhay ve cuoi file, di chuyen di 0 vi tri
  135.     (*n) = (ftell(f)+1)/sizeof(product); //ftell(); tra ve vi tri hien tai cua con tro
  136.     // SEEK_CUR: di chuyen bat dau tu vi tri hien tai cua con tro, chi dung trong fseek()
  137.     fseek(f,0,SEEK_SET); //Nhay ve dau file, di chuyen di 0 vi tri
  138.     fread(prd,sizeof(product),(*n),f);
  139.     fclose(f);
  140. }
  141.  
  142. void search_id(product prd[], int n, char id[]){
  143.     int i, check = 0;
  144.     for (i = 0; i < n; i++){
  145.         if (strcmp(id, prd[i].Id) == 0){
  146.             check = 1;
  147.             printf("%-10s %-10s %-15s %-10s %-10s\n", "Order", "Id", "Name", "Quantity", "Cost");
  148.             printf("%-10d %-10s %-15s %-10d %-10.2lf\n", i + 1, prd[i].Id, prd[i].name, prd[i].quantity, prd[i].cost);
  149.             break;
  150.         }
  151.         if (check == 0 && i == n - 1)
  152.             printf("Not found product have Id is %s !\n", id);
  153.     }
  154. }
  155.  
  156. void search_name(product prd[], int n, char name[]){
  157.     int i, check = 0;
  158.     for (i = 0; i < n; i++){
  159.         if (strcmp(name, prd[i].name) == 0){
  160.             check = 1;
  161.             printf("%-10s %-10s %-15s %-10s %-10s\n", "Order", "Id", "Name", "Quantity", "Cost");
  162.             printf("%-10d %-10s %-15s %-10d %-10.2lf\n", i + 1, prd[i].Id, prd[i].name, prd[i].quantity, prd[i].cost);
  163.             break;
  164.         }
  165.         if (check == 0 && i == n - 1)
  166.             printf("Not found product have name is %s !\n", name);
  167.     }
  168. }
  169.  
  170. void search(product prd[], int n){
  171.     int select;
  172.     char s[50];
  173.     do{
  174.         printf("\t1: Id\n\t2: Name\n\t3: exit search\nEnter the type you want search: ");
  175.         scanf("%d", &select);
  176.         gets(s);
  177.         switch(select){
  178.             case 1: {
  179.                 printf("Enter Id want search: ");
  180.                 char id[50];
  181.                 gets(id);
  182.                 search_id(prd, n, id);
  183.                 break;
  184.             }
  185.             case 2: {
  186.                 printf("Enter name want search: ");
  187.                 char name[50];
  188.                 gets(name);
  189.                 search_name(prd, n, name); break;
  190.             }
  191.             case 3: return;
  192.             default : printf("Error select !"); break;
  193.         }
  194.     }while (select != 3);
  195. }
Advertisement
Add Comment
Please, Sign In to add comment