Here is my problem: I want to map the file "filename.txt", which basically consists of two pairs of strings per line:
"string1 string2
string3 string4
string5 string6..."
and then I wanted to separate the different strings using strtok.
So I map the file like this:
// open file
if ((fdsrc = open("filename.txt", O_RDONLY)) < 0) {
fprintf(stderr, "src open error");
exit(1);
}
// get the size of the file
if (fstat(fdsrc, &statbuf) < 0) {
fprintf(stderr, "fstat error");
exit(1);
}
// mmap the file
if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdsrc, 0)) == (caddr_t) -1) {
fprintf(stderr, "mmap src");
exit(1);
}
When I run the line
printf("src: %s \n", src);
it prints the content of the file correctly!
But when I try to separate the words
char* token;
token = strtok(src, " \n");
while (token != NULL) {
token = strtok(NULL, " \n");
}
the output is Segmentation Fault. Why can't I use the StrTok then?