Advertisement
DrAungWinHtut

database.c

Feb 15th, 2023
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.06 KB | None | 0 0
  1. //
  2. // Created by bluephoenix on 2/11/2023.
  3. //
  4. #include <stdio.h>
  5. #include <string.h>
  6. struct STUDENT {
  7.     char name[20];
  8.     int age;
  9.     int student_id;
  10. };
  11.  
  12. int propagateEmptyData(FILE* fptr,struct STUDENT student_data,int count);
  13. int read4Print(FILE* fptr,struct STUDENT student_data);
  14. int bin2TextFile(FILE* fi,char *oFname,struct STUDENT student_data);
  15.  
  16. int main(){
  17.     int record =0 ;
  18.     struct STUDENT student_data = {"",0,0};
  19.     FILE *fptr = NULL;
  20.     char fname[]="d:\\data.dat";
  21.     fopen_s(&fptr,fname,"wb+");
  22.     if(fptr==NULL)
  23.     {
  24.         return 1;
  25.     }
  26.  
  27.     propagateEmptyData(fptr,student_data,5);
  28.     //read4Print(fptr,student_data);
  29.     bin2TextFile(fptr,"d:\\out.txt",student_data);
  30.  
  31.     fclose(fptr);
  32.     return 0;
  33. }
  34.  
  35. int propagateEmptyData(FILE* fptr,struct STUDENT student_data,int count)
  36. {
  37.     if(fptr==NULL)
  38.     {
  39.         printf("propagate error! no such file \n");
  40.         return 1;
  41.     }
  42.     //writing 100 empty records into file
  43.     rewind(fptr);
  44.     for(int record=1;record<=count;record++)
  45.     {
  46.         student_data.student_id = record;
  47.         strcpy(student_data.name,"aung");
  48.         student_data.age = record *10;
  49.         fseek(fptr,(record-1)*sizeof(struct STUDENT),SEEK_SET);
  50.         fwrite(&student_data,sizeof(struct STUDENT),1,fptr);
  51.     }
  52.     return 0;
  53. }
  54.  
  55. int read4Print(FILE* fptr,struct STUDENT student_data)
  56. {
  57.     if(fptr==NULL)
  58.     {
  59.         printf("read error! no such file \n");
  60.         return 1;
  61.     }
  62.     int record =1;
  63.     rewind(fptr);
  64.     while(!feof(fptr))
  65.     {
  66.         student_data.student_id = 0;
  67.         strcpy(student_data.name,"");
  68.         student_data.age = 0;
  69.         fseek(fptr,(record-1)*sizeof(struct STUDENT),SEEK_SET);
  70.         fread(&student_data,sizeof(struct STUDENT),1,fptr);
  71.         if(student_data.student_id!=0)
  72.         {
  73.             printf("id=%-6dname=%-10sage=%6d \n",student_data.student_id,student_data.name,student_data.age);
  74.         }
  75.  
  76.         record++;
  77.     }
  78.     return 0;
  79. }
  80.  
  81. int bin2TextFile(FILE *fi,char *oFname,struct STUDENT student_data)
  82. {
  83.     FILE * fo = NULL;
  84.     fopen_s(&fo,oFname,"w+");    //text file output
  85.     if(fi==NULL)
  86.     {
  87.         printf("read error! no such input file \n");
  88.         return 1;
  89.     }
  90.     if(fo==NULL)
  91.     {
  92.         printf("read error! no such output file \n");
  93.         return 1;
  94.     }
  95.  
  96.     int record =1;
  97.     rewind(fi);
  98.     while(!feof(fi))
  99.     {
  100.         student_data.student_id = 0;
  101.         strcpy(student_data.name,"");
  102.         student_data.age = 0;
  103.         fseek(fi,(record-1)*sizeof(struct STUDENT),SEEK_SET);
  104.         fread(&student_data,sizeof(struct STUDENT),1,fi);
  105.         if(student_data.student_id!=0)
  106.         {
  107.             if(record!=1)
  108.             {
  109.                 fprintf(fo,"\n");
  110.             }
  111.             printf("id=%-6dname=%-10sage=%6d \n",student_data.student_id,student_data.name,student_data.age);
  112.             fprintf(fo,"id=%-6dname=%-10sage=%6d",student_data.student_id,student_data.name,student_data.age);
  113.         }
  114.  
  115.         record++;
  116.     }
  117.  
  118.     fclose(fo);
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement