Thread: Segmentation Fault

  1. #1
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187

    Segmentation Fault

    Hey guys, I'm getting a segmentation fault and I'm trying to sort it out but I have no idea why I'm getting it.

    Code:
     char input[20]; 
      printf("Qual o .txt que deseja abrir ?\n");
      fgets(input,20,stdin);
      FILE *fp;
      input[strlen(input)-1] = '\0';
      fp = fopen(input,"r"); 
      if (fp==NULL)
      {printf("Nao foi possivel abrir o seu ficheiro\n");}
      else leadj(fp,18);
    In case I input a .txt that doesn't exist the printf works fine but when I do input a .txt that does exist the leadj(fp,18) is getting me a segmentation fault and I have no clue why taking in fact that if I do it on main it works

    Any ideas ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps the error lies in leadj.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Found out what my error is.
    leadj receives a document name and I was passing it the document's pointer.

    Thanks

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > leadj receives a document name and I was passing it the document's pointer.
    You were passing a FILE* to something expecting a char* ?

    Was your compiler complaining about it?

    Or were you just ignoring the warnings?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    It was giving me no warnings, just a segmentation fault.
    But it's all fixed now!
    Thanks

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Don't tell me you were trying to call the function without a prototype in scope.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    I was just passing the function the document pointer instead of the document itself.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes I know, but my question is how it failed to be spotted by the compiler BEFORE you tried to run it (and have it crash horribly).

    This was an easily avoided problem in a well-written program with a decent compiler.

    You see, if this isn't solved, you're just going to run into the same problem over and over.
    And we'll get bored of telling you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Quote Originally Posted by Salem View Post
    Yes I know, but my question is how it failed to be spotted by the compiler BEFORE you tried to run it (and have it crash horribly).

    This was an easily avoided problem in a well-written program with a decent compiler.

    You see, if this isn't solved, you're just going to run into the same problem over and over.
    And we'll get bored of telling you.
    I have no idea.
    Instead of writing the document pointer I wrote the document name and it worked.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Instead of writing the document pointer I wrote the document name and it worked.
    Great - now figure out how to make the compiler complain about the old version.

    If you don't, I guarantee you'll be spending most of your time chasing stupid bugs.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Are you still compiling at the command line? If so please show how you are now compiling your programs (show the complete command line for your compile).

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault
    By gums in forum C Programming
    Replies: 11
    Last Post: 05-03-2007, 07:06 AM
  2. Segmentation Fault :(
    By DarkDot in forum C++ Programming
    Replies: 39
    Last Post: 04-07-2007, 04:16 AM
  3. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 05:50 PM
  4. segmentation fault when using gcc
    By stodd04 in forum C Programming
    Replies: 6
    Last Post: 02-14-2005, 07:34 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM