#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main (int argc, char* argv[])
{
char* filename = argv[1];
/* Open the file for writing. If it exists, append to it;
otherwise, create a new file. */
int fd = open (filename, O_RDONLY | O_CREAT | O_APPEND, 0666);
// Reading file probleme
close (fd);
return 0;
}
My problem is that I can’t really find how to read to buffer. I have only ints in the file but how can I read from it to that buffer? I can’t find functions to achieve that.
The function you’re looking for is called “read”. You have to pass it a buffer you’ve already allocated previously, however. Something like this ought to work:
after that call, n will contain the number of bytes read from the fd (or 0 for none or less than 0 if an error occured).
If you have raw ints in that file, you could then access them kinda like this:
ibuffer is then an array of ints of length 1024/sizeof(int) containing the first n/sizeof(int) consecutive ints in fd. Strictly speaking this isn’t quite legal C, but then I haven’t seen an architecture lately where this wouldn’t have worked.