Advertisement
andruhovski

unix-0103a

Mar 19th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/stat.h>
  3. #include <sys/types.h>
  4. #include <fcntl.h>
  5. int fd;
  6. int f1()
  7. {
  8.     static int j = 1;
  9.     if (j > 10) return 0;
  10.     write(fd, &j, sizeof(int));
  11.     printf("write %d -- %d\n", fd, j++);
  12.     return 1;
  13. }
  14.  
  15. void f2()
  16. {
  17.     int i;
  18.     lseek(fd, -sizeof(int), 1);
  19.     read(fd, &i, sizeof(int));
  20.     printf("read %d -- %d\n", fd, i);
  21. }
  22. int main(int argc, char *argv[])
  23. {
  24.     if (argc < 2)
  25.         puts("Format: rw filename");
  26.     else   
  27.         {
  28.         fd = open(argv[1], O_CREAT | O_RDWR);
  29.         while (f1())
  30.             f2();
  31.         close(fd);
  32.     }
  33.         return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement