Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. #include <fcntl.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct entry
  6. {
  7. char *surname;
  8. char *name;
  9. } entry_t;
  10.  
  11. void data_create(char *filename);
  12. void pos_create(char *filename);
  13. void data_insert(char *filename, char entry);
  14. void pos_insert(char *filename, int num_of_bytes, int pos);
  15. void delete_data(char *filename);
  16. int bin_search(int total, int x);
  17. void display_data(char *filename);
  18.  
  19.  
  20.  
  21. int main(int argc, char*argv[])
  22. {
  23. data_create("data.bin");
  24. pos_create("pos.bin");
  25.  
  26.  
  27.  
  28.  
  29. }
  30.  
  31. void data_create(char *filename)
  32. {
  33. int fd1;
  34.  
  35. if ((fd1 = open(filename, O_CREAT| O_RDWR | O_TRUNC | O_APPEND | O_EXCL, S_IRWXU)) == -1) {
  36. write(2,"file error...\n",20);
  37. _exit(1);
  38.  
  39. }
  40. close(fd1);
  41.  
  42.  
  43. }
  44.  
  45. void pos_create(char *filename)
  46. {
  47. int fd2;
  48. if ((fd2 = open(filename, O_CREAT| O_RDWR | O_TRUNC | O_APPEND | O_EXCL, S_IRWXU)) == -1) {
  49. write(2,"file error...\n",20);
  50. _exit(1);
  51.  
  52. }
  53. close(fd2);
  54. }
  55.  
  56. void data_insert(char *filename, char entry)
  57. {
  58. int fd1;
  59. if((fd1 = open(filename, O_CREAT| O_RDWR | O_TRUNC | O_APPEND | O_EXCL, S_IRWXU)) == -1) {
  60. write(2,"file error...\n",20);
  61. _exit(1);
  62. }
  63.  
  64. lseek(fd1, 0, SEEK_END);
  65.  
  66. write(fd1, &entry, sizeof(char));
  67. close(fd1);
  68. }
  69.  
  70. void pos_insert(char *filename, int num_of_bytes, int pos)
  71. {
  72. int i;
  73. int filesize;
  74. int fd2;
  75.  
  76. if((fd2 = open(filename, O_CREAT| O_RDWR | O_TRUNC | O_APPEND | O_EXCL, S_IRWXU)) == -1) {
  77. write(2,"file error...\n",20);
  78. _exit(1);
  79. }
  80.  
  81. lseek(fd2, pos*sizeof(char), SEEK_SET);
  82. //filesize = strlen??
  83.  
  84. }
  85. int bin_search(int total, int x)
  86. {
  87. char left, right, mid;
  88.  
  89. mid = (left + right) / 2;
  90.  
  91. if(left>right)
  92. return -1;
  93.  
  94. if(x==mid)
  95. return mid;
  96.  
  97. if(x>mid)
  98. {
  99. x*=1,5; //giati thelw to miso tou misou apo deksia px an exw total=100 -> mid=50 -> mid tou mid=75 (50*1,5)
  100. return x;
  101. }
  102.  
  103. if(x<mid)
  104. {
  105. x*=0,5; //giati thelw to miso tou misou apo aristera px an exw total=100 -> mid=50 ->mid tou mid=25
  106. return x;
  107. }
  108.  
  109.  
  110.  
  111. }
  112.  
  113.  
  114. void delete_data(char *filename)
  115. {
  116. entry_t en;
  117.  
  118. int fd1;
  119. int n,i;
  120. char size[n];
  121. write(1,"Give a surname and a name",n);
  122. for(i=0; i<n; i++)
  123. {
  124. size[i]= read(fd1,&size,sizeof(char));
  125. }
  126. //remove() na brw lower entolh
  127.  
  128. }
  129.  
  130. void display_data(char *filename)
  131. {
  132. int fd1;
  133. char c;
  134. if(fd1 == NULL)
  135. {
  136. write(2, "cannot open file\n", 20);
  137. _exit(1);
  138. }
  139. c = read(1, fd1, sizeof(char));
  140.  
  141. while(c != EOF) //ti sto dialo einai sto <stdio.h> kai den to anagnwrizei?
  142. {
  143. write(2, &c, sizeof(char));
  144.  
  145. }
  146. close(fd1);
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement