Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdlib.h>
- typedef struct entry
- {
- char *surname;
- char *name;
- } entry_t;
- void data_create(char *filename);
- void pos_create(char *filename);
- void data_insert(char *filename, char entry);
- void pos_insert(char *filename, int num_of_bytes, int pos);
- void delete_data(char *filename);
- int bin_search(int total, int x);
- void display_data(char *filename);
- int main(int argc, char*argv[])
- {
- data_create("data.bin");
- pos_create("pos.bin");
- }
- void data_create(char *filename)
- {
- int fd1;
- if ((fd1 = open(filename, O_CREAT| O_RDWR | O_TRUNC | O_APPEND | O_EXCL, S_IRWXU)) == -1) {
- write(2,"file error...\n",20);
- _exit(1);
- }
- close(fd1);
- }
- void pos_create(char *filename)
- {
- int fd2;
- if ((fd2 = open(filename, O_CREAT| O_RDWR | O_TRUNC | O_APPEND | O_EXCL, S_IRWXU)) == -1) {
- write(2,"file error...\n",20);
- _exit(1);
- }
- close(fd2);
- }
- void data_insert(char *filename, char entry)
- {
- int fd1;
- if((fd1 = open(filename, O_CREAT| O_RDWR | O_TRUNC | O_APPEND | O_EXCL, S_IRWXU)) == -1) {
- write(2,"file error...\n",20);
- _exit(1);
- }
- lseek(fd1, 0, SEEK_END);
- write(fd1, &entry, sizeof(char));
- close(fd1);
- }
- void pos_insert(char *filename, int num_of_bytes, int pos)
- {
- int i;
- int filesize;
- int fd2;
- if((fd2 = open(filename, O_CREAT| O_RDWR | O_TRUNC | O_APPEND | O_EXCL, S_IRWXU)) == -1) {
- write(2,"file error...\n",20);
- _exit(1);
- }
- lseek(fd2, pos*sizeof(char), SEEK_SET);
- //filesize = strlen??
- }
- int bin_search(int total, int x)
- {
- char left, right, mid;
- mid = (left + right) / 2;
- if(left>right)
- return -1;
- if(x==mid)
- return mid;
- if(x>mid)
- {
- 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)
- return x;
- }
- if(x<mid)
- {
- x*=0,5; //giati thelw to miso tou misou apo aristera px an exw total=100 -> mid=50 ->mid tou mid=25
- return x;
- }
- }
- void delete_data(char *filename)
- {
- entry_t en;
- int fd1;
- int n,i;
- char size[n];
- write(1,"Give a surname and a name",n);
- for(i=0; i<n; i++)
- {
- size[i]= read(fd1,&size,sizeof(char));
- }
- //remove() na brw lower entolh
- }
- void display_data(char *filename)
- {
- int fd1;
- char c;
- if(fd1 == NULL)
- {
- write(2, "cannot open file\n", 20);
- _exit(1);
- }
- c = read(1, fd1, sizeof(char));
- while(c != EOF) //ti sto dialo einai sto <stdio.h> kai den to anagnwrizei?
- {
- write(2, &c, sizeof(char));
- }
- close(fd1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement