Seg fault, I do not understand
I am working on a project and one of the elements within the project is not working as expected. So I wrote a short program to test the functionality.
Code:
#include<fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <errno.h>
int main(int argc, char*argv ){
//Assume that /home/student/m/marescal/Desktop/echo.c is a file.
chdir("/home/student/m/marescal/Desktop");
printf("%s, \n",get_current_dir_name());
int v=open("/echo.c",O_RDONLY,0);//Put a file in your home directory here!
if (errno==0){
printf("Success");
}
else{ printf("an error has occurred \n");
}
}
when I comment out the chdir() function it works fine (I am in the directory I am trying to access the file from) so I have 2 questions
1. This never gives the success message, why??
2. When the else is in there, it gives a seg fault.