Seg fault, I do not understand

This is a discussion on Seg fault, I do not understand within the C Programming forums, part of the General Programming Boards category; I am working on a project and one of the elements within the project is not working as expected. So ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    29

    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.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Did you check the return from chdir() for an error?
    (that is; did you change to the directory you wanted, if not you will not find the file you are looking for, so why try to open it?)

    When using strings and you must use 2 slashes ('\\') to denote one slash (and the backslashes).

    ie "c:\\SomeFolder\\AnotherFolder\\TheFile.ext"

    This is because a backslash denotes a special character pair, the second in the pair denoting which special character it is (ie \t is TAB, \n is new line, \\ is \)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,024
    This appear to be a linux environment, so the \\ advice may not apply. That said, however, you are sure that the problem isn't "/echo.c"? Seeing as the "/" tells it to look in the root folder.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Yep! it looks like the o/p omitted the the leading dot while specifying the relative path ie "./echo.c"

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    nope, still does not work!

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Then echo.c doesn't exist in your home dir; btw why aren't you checking the return value "v" from open() for a valid file descriptor instead of testing errno.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    gcc -g mycode.c
    gdb ./a.out

    when gdb starts, type "run". You will find out exactly what causes the segfault.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Testing errno to determine if an error happened is incorrect. Instead you must check the return values of the functions themselves. ONLY IF the function returns an error are you allowed to check errno.

    In other words, the fact that errno != 0 is totally meaningless since you haven't bothered to check the return status of open(). Of course, there are other problems in your code that were already pointed out.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault Help
    By sedition in forum C Programming
    Replies: 11
    Last Post: 07-14-2009, 06:26 AM
  2. unknown seg fault after malloc
    By seaking1 in forum C Programming
    Replies: 4
    Last Post: 02-25-2009, 07:51 PM
  3. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 05:27 AM
  4. seg fault
    By hka26 in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2007, 02:38 AM
  5. Seg fault on string pointer
    By Cikotic in forum C Programming
    Replies: 15
    Last Post: 03-14-2004, 03:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21