I am trying to write a program where I can copy an int
into a char
array, and then write that char
array into a file using the write
syscall. In another program I want to read the contents of the file into a char
array, and then retrieve the int
from the array. The thing that I am having trouble understanding is how to write the contents of the int
into the array and how to read the int
from the char array.
Here is what I have
writing to file:
int fd2 = open("file2.txt", O_RDWR | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH);
//create buff2 with 1024 chars and last 4 bytes as int
write(fd2, buff2, 1028);
I'm not sure how to exactly implement the commented line
reading from file:
int fd2 = open("file2.txt", O_RDWR | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH);
char buff2[1028];
read(fd2, buff2, 1028);
int val = *((int)((buff2+1023)+sizeof(int))); // file contains exactly 1028 bytes. first 1024 are characters, next 4 is int
printf("%d\n", val);
The line where I am trying to read the integer gives me a compiler error saying invalid type argument of unary '*' (have 'int')
I would appreciate any help