Thread: segmentation fault

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    23

    segmentation fault

    Code:
    		
                    if (search == 0)
    		{
    			non_linux = non_linux + 1; // just a counter
    			printf("'%s' was not found in the linux or user dictionary\n", compare_word);
    			printf("Do you want to add this word to your dictionary(1) yes, (0) no.\n");
    			scanf("%d", &answer);
    			
    			if(answer == 1)
    			{
    				if(user_dictionary == NULL)
    				{
    					// prompt user one last time for the dictionary
    					printf("File does not exist, one wil be created and appened the word");
    					user_dictionary = fopen(dictionary_filename, "a+");	
    					fprintf(user_dictionary, "%s ", compare_word); // HERE IS WHERE THE SEGFAULT IS
    					fprintf(out, "%s%c ", compare_word, punct); // just some output
    				}
    				
    				else
    				{
    					printf("File exists and will have the new word saved.\n");
    					fprintf(user_dictionary, "%s ", compare_word);
    					fprintf(out, "%s%c ", compare_word, punct);
    				}
    			}
    so as i stated that is where it is, and i have no idea how to stop it? I need to open the user_dictionary append, read and write. and i need to write that word at the end of the file. but i always get a segfault. I commented it out and it worked fine, but i need to be able to write that word in there in order for it to stop asking the user if he wants to add it in.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Did you check to make sure the fopen() happens?

    Code:
    #include <errno.h>
    #include <string.h>
    
    if (!(user_dictionary = fopen(dictionary_filename, "a+"))) 
            perror("fopen fail:");
    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

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You never check to see if the file opens successfully. If you aren't closing it after using it elsewhere, your open will fail here.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    23
    I used to, but i got tired of asking the user haha, so by this point i just open it with "a+" which should append to read and write and it will create its own file right?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    How is user_dictionary defined?
    How is dictionary_filename defined?
    How is compare_word defined?

    You are not checking the return value of fopen() so you don't actually know if the file is open or not.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    23
    but when i ran it through gdb it says the fprintf is the one seg faulting and when i comment it out it will still run, so it couldnt be the fopen?

    compare_word is a array of characters from another inputted file. user_dictionary is a file pointer to the file i am trying to append, and dictionary_filename is a file pointer to a linux dictionary i have
    Last edited by deepseawolf; 03-13-2011 at 05:53 PM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by deepseawolf View Post
    I used to, but i got tired of asking the user haha, so by this point i just open it with "a+" which should append to read and write and it will create its own file right?
    The point is not to ask the user anything the point is to check the return value of the function call in user_dictionary to know if the file open activity succeeded or not.

    Code:
    FILE *user_dictionary;
    
    user_dictionary = fopen("filename","a");
    if (! user_dictionary)
      { puts ("Holy crap the file didn't open");
         exit (1); }

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by deepseawolf View Post
    but when i ran it through gdb it says the fprintf is the one seg faulting and when i comment it out it will still run, so it couldnt be the fopen
    No. If it fails to open, you are trying to write to an invalid file descriptor, so it's unsurprising that it crashes.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    23
    Code:
    					if (user_dictionary == NULL)
    					{
    					printf("test if it opened.\n");
    					}
    ya i just add that after my "user_dictionary = fopen(dictionary_filename, "a+");"
    if its openning, why isnt my "fprintf(user_dictionary, "%s ", compare_word);" working?

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by deepseawolf View Post
    but when i ran it through gdb it says the fprintf is the one seg faulting and when i comment it out it will still run, so it couldnt be the fopen?
    Yes, probably because you are trying to print to a NULL handle because fopen() failed.

    If you comment it out it will still work because (at least in your snippet), that file pointer is not used anywhere else.

    You have to check and see if the file is open or not. If not, that is the the problem, mystery solved.
    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

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    23
    oh crap... nvm if its NULL that means it didnt open, CRAP, whats wrong with my coding then if it isnt opening?

    if they answer one. it the prompts them to input a file name.
    Code:
    user_dictionary = fopen(dictionary_filename, "a+");
    if that code isnt openening the file then what else can i do?

    and boom goes the dynamite, so i forgot to ask for the file name again, thank you every one for beating it out of me. Last day antics .
    Last edited by deepseawolf; 03-13-2011 at 06:03 PM.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by deepseawolf View Post
    Code:
    					if (user_dictionary == NULL)
    					{
    					printf("test if it opened.\n");
    					}
    Quite possibly this never prints because stdout has not been flushed when the seg fault happens. Ie, user_dictionary is NULL, and the message is in the stdout buffer when the crash happens (so does not get sent to the console). Always print to stderr (which is unbuffered) for debugging:

    Code:
    fprintf(stderr,"test if it opened.\n");
    And try that way. If it still seems the file has opened properly, check the value of compare_word in gdb after the fault:

    Code:
    gdb> print compare_word
    Could be that is going out of bounds.
    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

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should only open it once. Never again. Either that, or in the same scope you open it, after you are done writing/reading, then close it, so you can open it again.

    Simple C rules: If you open it, close it when you are done. If you allocate it, free it when you are done.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Feb 2011
    Posts
    23
    gotcha, thank you every one

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by deepseawolf View Post
    oh crap... nvm if its NULL that means it didnt open, CRAP, whats wrong with my coding then if it isnt opening?

    if they answer one. it the prompts them to input a file name.
    Code:
    user_dictionary = fopen(dictionary_filename, "a+");
    if that code isnt openening the file then what else can i do?

    and boom goes the dynamite, so i forgot to ask for the file name again, thank you every one for beating it out of me. Last day antics .
    Now... one more thing... if dictionary_filename is null (i.e. if your user presses enter without inputting anything) your fopen is going to fail... so you need to check the filename input by the user...

    Error checking (within reason) is your friend....

    And to expand on Quzah's rules... NEVER keep a file open or hang on to memory any longer than you have to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM