I'm trying to parse the filenames in a directory into file name and extension. I'm using strtok to do the parsing but I keep getting an Segmentation fault. My code is below. What am I doing wrong?
Thanks,
#include <dirent.h>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
int main(int argc, char *argv[]) {
DIR *dp;
struct dirent *dirp;
struct stat buf;
char *str1;
char *filename;
if ((dp = opendir(argv[1])) == NULL)
printf("can't open %s", argv[1]);
while ((dirp = readdir(dp)) != NULL){
filename = dirp->d_name;
str1 = strtok(filename,".");
printf("%s\n",str1);
str1 = strtok(NULL,".");}
closedir(dp);
exit(0);
}
Thanks,
#include <dirent.h>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
int main(int argc, char *argv[]) {
DIR *dp;
struct dirent *dirp;
struct stat buf;
char *str1;
char *filename;
if ((dp = opendir(argv[1])) == NULL)
printf("can't open %s", argv[1]);
while ((dirp = readdir(dp)) != NULL){
filename = dirp->d_name;
str1 = strtok(filename,".");
printf("%s\n",str1);
str1 = strtok(NULL,".");}
closedir(dp);
exit(0);
}