Weird fopen() segfault

This is a discussion on Weird fopen() segfault within the C Programming forums, part of the General Programming Boards category; Ok, i've googled this and searched on the forums already and didn't find anything that really helped. This code is ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    Weird fopen() segfault

    Ok, i've googled this and searched on the forums already and didn't find anything that really helped. This code is segfaulting right after the sleep() call.

    Code:
    #include <stdio.h>
    #include <string.h>
    	
    int main(int argc, char *argv[]) {
    	FILE *fp, *ofp;
    	char *mode = 'r';
    	fprintf(stderr, "Sleeping\n");
    	printf("Trying to open %s\n",argv[1]);
    	sleep(3);
    	if ((fp = fopen("index.html", mode)) == EOF) { //tried argv[1] where index.html is too
    		fprintf(stderr, "File Not Found\n");
    		exit(1);
    	}
    }
    Code:
    ./getprx index.html
    Sleeping
    Segmentation fault
    Index.html exists in the directory the program is being run, I tried using the full path also that didn't help.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Try this:

    if ((fp = fopen("index.html", 'r')) == EOF)

    By getting rid of the pointer to r and just using r.

    Todd

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Don't you get warnings about converting integers to pointers without a cast? Have you tried
    Code:
    char *mode = "r";

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Also, I wouldn't use EOF as a condition to tell if the file opened properly.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you enable a minimal level of warnings when you compile that code, it would tell you that you are assigning an integer into a pointer (char *mode = 'r'). This will directly cause a segfault when the fopen() function tries to determine what your mode is, and tries to read memory at address 114 (the ascii-value of 'r').

    --
    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.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Read documentation with regard to the parameters, the return value, and the noteworthy behavior of the functions you plan on using.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    Yeah that was actually a really stupid mistake. I don't know C very well, just migrating over from Java and was expecting the compiler to give me more errors. I should probably turn that up so I don't get lost in runtime errors like this.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Java protects you from yourself a lot more effectively than C does. C will let you do all kinds of stupid things without even blinking, since it assumes you know what you're doing and just need a safety net for some things.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    16

    Smile

    Try this, it may solve your problem ..
    if ((fp = fopen("index.html", 'r')) == NULL)

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by krishna View Post
    Try this, it may solve your problem ..
    if ((fp = fopen("index.html", 'r')) == NULL)
    No, it will not

    Code:
    if ((fp = fopen("index.html", "r")) == NULL)
    should be
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    16

    Lightbulb

    thanks for this correction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird things with my linked list of queue
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 10:23 PM
  2. malloc() resulting in a SegFault?!
    By cipher82 in forum C++ Programming
    Replies: 21
    Last Post: 09-18-2008, 11:24 AM
  3. use of printf prevents segfault!
    By MK27 in forum C Programming
    Replies: 31
    Last Post: 08-27-2008, 12:38 PM
  4. weird segfault in fgets in process
    By 911help in forum C Programming
    Replies: 10
    Last Post: 12-29-2007, 12:51 PM
  5. help with stat() and fopen()
    By movl0x1 in forum C Programming
    Replies: 6
    Last Post: 07-25-2007, 05:28 AM

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