problem with sample program

This is a discussion on problem with sample program within the C Programming forums, part of the General Programming Boards category; i have a problem with the example program in the beginners C 'Command Line Arguments' tutorial. i have the code ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    5

    problem with sample program

    i have a problem with the example program in the beginners C 'Command Line Arguments' tutorial.

    i have the code exactly as is provided on the page. compile with gcc with no errors.
    the code works all fine, except when i run it with an unexisting filename, it then displays the message correctly, but gives after that a segmentation fault error:

    Could not open file
    [1] 13302 segmentation fault (core dumped)

    since i'm just beginning with programming, i would like to know what is going wrong here.
    if needed, i can paste the code here, but i let it out for now because it's on the site too.
    http://www.cprogramming.com/tutorial/c/lesson14.html

    thanx in advance,
    brabo.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    Move the fclose() call up a line.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    5
    is that because it was outside the curly brackets in which the variable 'file was declared?

    grtz,
    brabo

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, it's because it tries to close the file even when it wasn't opened correctly. If you try to use a variable outside the braces where it's declared, the compiler will tell you.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    5
    please bear with me.
    why was the file not opened correctly and how did moving up fclose() solve that?

    grtz,
    brabo.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brabo View Post
    please bear with me.
    why was the file not opened correctly and how did moving up fclose() solve that?

    grtz,
    brabo.
    I don't know why the file wasn't opened correctly, and moving the close doesn's solve THAT, it solves the crashing when you try to use a FILE pointer that is NULL (which leads to segmentation fault).

    What filename are you actually giving, and what output do you get?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    22
    The sample code has an if statement that first checks to see if the file was able to be opened. The else statement only executes if the file was able to be opened. However, because the fclose statement is outside of the if...else control structure, it will try and (write and) close the file whether it was able to open it or not. By placing it in the else statement (which should only execute if the file was able to open), you are preventing the error that happens when trying to (write and) close a File that was unable to be opened in the first place (probably because it did not exist).

    Hope that helps.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    5
    any non-existent filename gives the segmentation fault error.
    the output is as shown in my first post.

    Could not open file
    [1] 13302 segmentation fault (core dumped)

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    22
    Yes. When fopen goes to open the said file, if it does not exist, it returns the address of NULL instead of the address of said file (It doesn't exist). You cannot fclose (write and close) a file at address 0. It will give a segmentation fault (You can't write anything to a non-existing block of memory.

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    22
    Make the change suggested by Salem and you should no longer get the error. If the file doesn't exist it should print "Could not open file" to the screen.

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    5
    ah yes, it idd solved it. but i now understand why the move of fclose resolved it.
    thank you for your explanation gwarf420.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-01-2008, 11:56 PM
  2. I have finished my program, one problem
    By sloopy in forum C Programming
    Replies: 4
    Last Post: 11-29-2005, 01:10 AM
  3. Some Problem With My Program
    By Americano in forum C Programming
    Replies: 5
    Last Post: 10-18-2003, 01:58 AM
  4. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM

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