Newbie question: problems with fopen

This is a discussion on Newbie question: problems with fopen within the C Programming forums, part of the General Programming Boards category; Hi all, I get an segmentation fault truin to compile this code. What I'm doing wrong?. Thanks. Code: #include <stdio.h> ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    5

    Post Newbie question: problems with fopen

    Hi all, I get an segmentation fault truin to compile this code. What I'm doing wrong?. Thanks.

    Code:
    #include <stdio.h>
    
    int i;
    int letter;
    FILE *fp;
    
    main(int argc, char *argv[])
    
    {
    if (fp = fopen("test.txt", "a") == NULL) {
    	printf("Couldn't open file\n");
    	} else {
    	printf("Open file\n");
    	letter = putc(65, fp);
    }
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Here you're trying to do too much at once and doing it wrong.
    Code:
    if (fp = fopen("test.txt", "a") == NULL) {
    Rather than show the correct syntax for the one-liner, I'd advise breaking it up into separate lines of code. And if the file does not open, use perror to find out why.

    And the rest leaves others room to comment...
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by Ikim
    Code:
    if (fp = fopen("test.txt", "a") == NULL) {
    This is the line where the problem is. "=" binds less tightly than "==", so what you have is evaluated as
    Code:
    if (fp = (fopen("test.txt", "a") == NULL)) {
    If the open succeeds, fp gets the value 0, and if it fails, fp is 1. I'm surprised that you're not getting a compiler warning (integer converted to pointer without typecast). What you want is
    Code:
    if ((fp = fopen("test.txt", "a")) == NULL) {
    Insert obnoxious but pithy remark here

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    5
    Thanks, a lot, It works now. Thanks

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    But since no one has yet taken the bait, your main should be explicitly declared as returning an int (1989 was a long time ago), and return an int you should (usually 0). And using 65 rather that 'A' is merely obfuscatory. And like I tell my 11-year-old, "If you open it, then close it when you're done, ...". And proper indentation is more beneficial to newbs than to those who will simply hit a "beautify" button. And don't use globals when you don't need to. And consider using puts when you are simply going to put a string to the stdout (rather than needing to print a formatted string). And if fopen succeeds, it is not fp that will be 1 (most likely), but the comparison to NULL that will either be 1 or 0.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by Dave_Sinkula
    And if fopen succeeds, it is not fp that will be 1 (most likely), but the comparison to NULL that will either be 1 or 0.
    Actually, as the code was originally written, fp would have been 1 or 0, because the expression (within the if parens) evaluated to:
    fp = (fopen(filename, mode) == NULL). Since the equality operator (==) results in a boolean result that is represented (in C) as 0 (for false) or 1 (for true), and fp is being assigned that result, it would indeed have such a value.

    And yes, I know that NULL == 0 by definition, even if the CPU representation of a NULL pointer is not a pattern with the value 0.
    Insert obnoxious but pithy remark here

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Ah, yes. I was not reading things in context. Mea culpa.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Actually, as the code was originally written, fp would have been 1 or 0, because the expression (within the if parens) evaluated to:
    fp = (fopen(filename, mode) == NULL). Since the equality operator (==) results in a boolean result that is represented (in C) as 0 (for false) or 1 (for true), and fp is being assigned that result, it would indeed have such a value.
    Is that right? I thought == returned 0 or a non-zero value, not 0 or 1.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    old man
    Join Date
    Dec 2005
    Posts
    90
    Quote Originally Posted by dwks
    Is that right? I thought == returned 0 or a non-zero value, not 0 or 1.
    According to the standard, it's 0 or 1.

    I see your point, though ... it shouldn't really matter.

  10. #10
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by dwks
    Is that right? I thought == returned 0 or a non-zero value, not 0 or 1.
    Any non-zero integral value evaluates, in a boolean context, to true, but C defines boolean true to promote to an integral type as 1. False as 0. It's not really a symmetric relationship.
    Insert obnoxious but pithy remark here

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    And then if you want to really confuse yourself, the return value of 0 from strcmp means your match was true.


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

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by dwks
    Is that right? I thought == returned 0 or a non-zero value, not 0 or 1.
    Non-zero is "on input". The result of the equality operators is 0 or 1.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  3. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  4. newbie question dealing with an error
    By zack787 in forum C Programming
    Replies: 2
    Last Post: 02-27-2008, 03:49 PM
  5. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 03:09 PM

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