Im writing a program in C (eclipse in linux) so I need to open a big text file and read it (and than try with diffrent size of buffer each time)
Anyways, this is the code and I don't understand why Im getting segmentation fault from the open function
int main(void)
{
int fd;
char* buff[67108864];
FILE *testfile;
double dif;
fd = open("testfile.txt", O_RDONLY);
if (fd>=0) {
read(fd,buff,67108864);
close(fd); }
return 0;
}
I have edited my question but now if I change my buffer to the biggest size I need (67108864 bytes) im still getting segmentation fault...
should be a pointer
Also after reading
read(fd,buff,(sizeof(char)));
you should allocate more memory to buff with realloc.change
to
what you need is a char array, not a char point array.
well if you want to allocate memory into buff, you need to make it a pointer..
also notice you only allocate one char.. you should consider that, I think you want to use more memory..
another common thing is not using dynamic memory for file reading..
try:
and then just the same code...
and then just keep reading until the find is complete, read returns the amount of bytes actually read.
Also like commented above, you are using testfile before initializing it.. that is also an access violation