Segmentation fault ( core dumped ) on linux, could someone check for me

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
int main(int argc, char* argv[])
{
DIR *p;
struct dirent *f;
struct stat show;
p = opendir(argv[1]);
while((f = readdir(p)) != NULL)
{
printf("%s\t", f->d_name);
lstat(f->d_name, &show);
// directory
if(S_ISDIR(show.st_mode)){
printf("d");
}
else{
printf("-");
}
//user
if((show.st_mode & S_IRUSR) == S_IRUSR){
printf("r");
}
else{
printf("-");
}
if((show.st_mode & S_IWUSR) == S_IWUSR){
printf("w");
}
else{
printf("-");
}
if((show.st_mode & S_IXUSR) == S_IXUSR){
printf("x");
}
else{
printf("-");
}
//group
if((show.st_mode & S_IRGRP) == S_IRGRP){
printf("r");
}
else{
printf("-");
}
if((show.st_mode & S_IWGRP) == S_IWGRP){
printf("w");
}
else{
printf("-");
}
if((show.st_mode & S_IXGRP) == S_IXGRP){
printf("x");
}
else{
printf("-");
}
//other
if((show.st_mode & S_IROTH) == S_IROTH){
printf("r");
}
else{
printf("-");
}
if((show.st_mode & S_IWOTH) == S_IWOTH){
printf("w");
}
else{
printf("-");
}
if((show.st_mode & S_IXOTH) == S_IXOTH){
printf("x");
}
else{
printf("-");
}
printf("\t%d\t%d\t%d\t%d\t%d\n", show.st_uid, show.st_gid, show.st_size, show.st_mtime);
}
}
Last edited on
it's only the simple code for checking the status of the file in linux operation, but why still have " Segmentation fault ( core dumped ) " in the end after I do on " ./a.out " operation
First, Please always use code tags, edit your post, select all the code, press the <> button on the right under format.

This part won't work if you don't provide arguments to your program:

p = opendir(argv[1]);

Does it work when you do provide a directory name as an argument?

Do the functions you have used return values you can check to see how well they worked?

Investigate learning to use a debugger, Hopefully there is one in your IDE (if using one) or try gdb. This is by far the easiest way to fix run time problems.

Hope all goes well.

P.S that was only a few suggestions - there may be lots of others things I hadn't seen.
Topic archived. No new replies allowed.