Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <fcntl.h>
  5. #include <string.h>
  6.  
  7. int get_line(int fd, char *line, int line_no, int max_length, int *line_length)
  8. {
  9. lseek(fd, 0, SEEK_SET);
  10. int poz = 0;
  11. int l = 1;
  12. char c;
  13. while(read(fd,&c, 1) != 0)
  14. {
  15. if(c == '\n')
  16. {
  17. if(l == line_no)
  18. {
  19. *line_length = poz+1;
  20. return 0;
  21. }
  22. l++;
  23. poz = 0;
  24. }
  25. else{
  26. if(l == line_no && poz < max_length)
  27. line[poz] = c;
  28.  
  29. poz++;
  30. }
  31. }
  32. return -1;
  33. }
  34.  
  35. typedef struct _OPERATION {
  36. int no1;
  37. int no2;
  38. char operator;
  39. } OPERATION;
  40.  
  41. OPERATION a,b,c;
  42.  
  43. void write_operation(int fd, OPERATION *op)
  44. {
  45. write(fd, op, sizeof(OPERATION));
  46. }
  47. void read_operation(int fd, OPERATION *op)
  48. {
  49. read(fd, op, sizeof(OPERATION));
  50. }
  51.  
  52. void perform_operations(int fd)
  53. {
  54. OPERATION a;
  55. while(read(fd, &a, sizeof(OPERATION)))
  56. {
  57. printf("%d %c %d\n", a.no1, a.operator, a.no2 );
  58. }
  59. }
  60.  
  61. int main()
  62. {
  63. // int fd = open("read.txt",O_RDWR);
  64. // a.no1 = 5;
  65. // a.no2 = 5;
  66. // a.operator = '+';
  67. // write_operation(fd, &a);
  68. // lseek(fd, 0, SEEK_SET);
  69. // read_operation(fd, &b);
  70. // printf("%d%d\n", b.no1, b.no2 );
  71. int fd = open("operatii.bin", O_RDWR);
  72. perform_operations(fd);
  73. return 0;#include <stdio.h>
  74. #include <unistd.h>
  75. #include <sys/types.h>
  76. #include <fcntl.h>
  77. #include <string.h>
  78.  
  79. int get_line(int fd, char *line, int line_no, int max_length, int *line_length)
  80. {
  81. lseek(fd, 0, SEEK_SET);
  82. int poz = 0;
  83. int l = 1;
  84. char c;
  85. while(read(fd,&c, 1) != 0)
  86. {
  87. if(c == '\n')
  88. {
  89. if(l == line_no)
  90. {
  91. *line_length = poz+1;
  92. return 0;
  93. }
  94. l++;
  95. poz = 0;
  96. }
  97. else{
  98. if(l == line_no && poz < max_length)
  99. line[poz] = c;
  100.  
  101. poz++;
  102. }
  103. }
  104. return -1;
  105. }
  106.  
  107. typedef struct _OPERATION {
  108. int no1;
  109. int no2;
  110. char operator;
  111. } OPERATION;
  112.  
  113. OPERATION a,b,c;
  114.  
  115. void write_operation(int fd, OPERATION *op)
  116. {
  117. write(fd, op, sizeof(OPERATION));
  118. }
  119. void read_operation(int fd, OPERATION *op)
  120. {
  121. read(fd, op, sizeof(OPERATION));
  122. }
  123.  
  124. void perform_operations(int fd)
  125. {
  126. OPERATION a;
  127. while(read(fd, &a, sizeof(OPERATION)))
  128. {
  129. printf("%d %c %d\n", a.no1, a.operator, a.no2 );
  130. }
  131. }
  132.  
  133. int main()
  134. {
  135. // int fd = open("read.txt",O_RDWR);
  136. // a.no1 = 5;
  137. // a.no2 = 5;
  138. // a.operator = '+';
  139. // write_operation(fd, &a);
  140. // lseek(fd, 0, SEEK_SET);
  141. // read_operation(fd, &b);
  142. // printf("%d%d\n", b.no1, b.no2 );
  143. int fd = open("operatii.bin", O_RDWR);
  144. perform_operations(fd);
  145. return 0;
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement