I want to open a file, read its content and store it in an array using C code.
I did it on my Windows laptop and it works but when i try the code on my Raspberrypi i get segmentation faults. I've been trying for a while to debugm I'm quite new to C so I'm having trouble finding what I did wrong.
char *readFile(char *fileName)
{
FILE *ptr_file;
char *ptr_data;
int n = 0;
char c;
ptr_file = fopen(fileName, "r");
if(ptr_file == NULL)
{
perror("File could not be opened.\n");
exit(EXIT_FAILURE);
}
fseek(ptr_file, 0, SEEK_END);
long f_size = ftell(ptr_file);
fseek(ptr_file,0, SEEK_SET);
ptr_data = (char *)malloc(f_size+1);
if(ptr_data == NULL)
{
perror("MALLOC FAILED");
exit(EXIT_FAILURE);
}
while((c = fgetc(ptr_file)) != EOF)
{
ptr_data[n++] = (char)c;
}
ptr_data[n] = '\0';
fclose(ptr_file);
return ptr_data;
}
to me it seems like the segmentations fault appears in the while loop after the call to malloc
.
Why does it work on my laptop and not on the raspberrypi?
at the same time i dont understand why i get segmentation faults on my RPi if id do this:
int main(int argc, char *argv[])
{
char data[100] = {};
FILE *ptr_file;
char *ptr_data=data;
int n = 0, i = 0;
char c;
ptr_file = fopen(fileName, "r");
if(ptr_file == NULL)
{
perror("File could not be opened.\n");
exit(EXIT_FAILURE);
}
while((c = fgetc(ptr_file)) != EOF)
{
ptr_data[n++] = (char)c;
}
ptr_data[n] = '\0';
while(i <n,i++)
{
printf("%c\n",data[i]);
fclose(ptr_file);
}
return 0; }
Maybe you should disable Linux memory overcommit. See this.
BTW, you might consider using open(2) to open your file, fstat(2) to get statistics, notably file size, about it, then mmap(2) to project the file into virtual memory by growing your address space.
Then use
ad
instead ofptr_data
(which becomes useless). Don't forget to callmunmap
andclose
when you are done...You could
close
just after themmap
if you want to.Read Advanced Linux Programming.
There are some issues when reading text file on different environment. When writing a new line, for example, may consume 2 bytes on Windows, and just 1 on Linux. From an article:
In another words, fseek and ftell function behavior may be different depending on which environment you're working with.
For further explanation you may read this topic: https://www.securecoding.cert.org/confluence/display/seccode/FIO14-C.+Understand+the+difference+between+text+mode+and+binary+mode+with+file+streams