Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <string.h>
- int get_line(int fd, char *line, int line_no, int max_length, int *line_length)
- {
- lseek(fd, 0, SEEK_SET);
- int poz = 0;
- int l = 1;
- char c;
- while(read(fd,&c, 1) != 0)
- {
- if(c == '\n')
- {
- if(l == line_no)
- {
- *line_length = poz+1;
- return 0;
- }
- l++;
- poz = 0;
- }
- else{
- if(l == line_no && poz < max_length)
- line[poz] = c;
- poz++;
- }
- }
- return -1;
- }
- typedef struct _OPERATION {
- int no1;
- int no2;
- char operator;
- } OPERATION;
- OPERATION a,b,c;
- void write_operation(int fd, OPERATION *op)
- {
- write(fd, op, sizeof(OPERATION));
- }
- void read_operation(int fd, OPERATION *op)
- {
- read(fd, op, sizeof(OPERATION));
- }
- void perform_operations(int fd)
- {
- OPERATION a;
- while(read(fd, &a, sizeof(OPERATION)))
- {
- printf("%d %c %d\n", a.no1, a.operator, a.no2 );
- }
- }
- int main()
- {
- // int fd = open("read.txt",O_RDWR);
- // a.no1 = 5;
- // a.no2 = 5;
- // a.operator = '+';
- // write_operation(fd, &a);
- // lseek(fd, 0, SEEK_SET);
- // read_operation(fd, &b);
- // printf("%d%d\n", b.no1, b.no2 );
- int fd = open("operatii.bin", O_RDWR);
- perform_operations(fd);
- return 0;#include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <string.h>
- int get_line(int fd, char *line, int line_no, int max_length, int *line_length)
- {
- lseek(fd, 0, SEEK_SET);
- int poz = 0;
- int l = 1;
- char c;
- while(read(fd,&c, 1) != 0)
- {
- if(c == '\n')
- {
- if(l == line_no)
- {
- *line_length = poz+1;
- return 0;
- }
- l++;
- poz = 0;
- }
- else{
- if(l == line_no && poz < max_length)
- line[poz] = c;
- poz++;
- }
- }
- return -1;
- }
- typedef struct _OPERATION {
- int no1;
- int no2;
- char operator;
- } OPERATION;
- OPERATION a,b,c;
- void write_operation(int fd, OPERATION *op)
- {
- write(fd, op, sizeof(OPERATION));
- }
- void read_operation(int fd, OPERATION *op)
- {
- read(fd, op, sizeof(OPERATION));
- }
- void perform_operations(int fd)
- {
- OPERATION a;
- while(read(fd, &a, sizeof(OPERATION)))
- {
- printf("%d %c %d\n", a.no1, a.operator, a.no2 );
- }
- }
- int main()
- {
- // int fd = open("read.txt",O_RDWR);
- // a.no1 = 5;
- // a.no2 = 5;
- // a.operator = '+';
- // write_operation(fd, &a);
- // lseek(fd, 0, SEEK_SET);
- // read_operation(fd, &b);
- // printf("%d%d\n", b.no1, b.no2 );
- int fd = open("operatii.bin", O_RDWR);
- perform_operations(fd);
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement