Thread: cannot open file

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    85

    cannot open file

    hello everyone
    i am having an issue with opening a file. it is meant to open the file in a web browser...

    detailed
    i am writing a webserver everything works apart from when i try to open a 404.html error file
    the permissions are rwx-rwx-rwx and not assigned to root for 404.html

    i have no idea why this doesnt work! everything looks fine

    Code:
    strcpy(resource,webroot);
    if(fd1 == -1)
    {
    		printf("404 error fd1 = %d\n", fd1);
    		memset(ptr,0,1);
    		strcpy(ptr,"404.html");
    		strcpy(resource,errors);
    		strcat(resource,ptr);
    		fd1 = open(resource,O_RDONLY,0);				
    		printf("Opening \"%s\"%d\n",resource,fd1);
    }
    all help is much appreciated
    instead of printing the 404 error i get an error from firefox saying connection was reset when on terminal the program prints it has sent the 404.html file and the program is still running
    Last edited by aadil7; 03-22-2011 at 05:44 AM.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    any help or am i helpless?
    i know the coding is right but i am just baffled thats all
    if i cant get help ill just have to start from ground 0

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Did you mean for this:
    strcpy(resource,errors);

    to be this?
    strcpy(resource,"error");

    It's hard to tell if the pyramids of Giza are built right or not, looking at them through a straw. Print out and watch the value of resource, just before the call to open, and see what's in there for char's, especially an illegal or incorrect, filename char.

  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
    Rather than
    Code:
    		memset(ptr,0,1);
    		strcpy(ptr,"404.html");
    		strcpy(resource,errors);
    		strcat(resource,ptr);

    How about
    Code:
    		strcpy(resource,errors);
    		strcat(resource,"404.html");
    And if you can't open the filename, it's generally a good idea to print the filename itself.
    Are you missing a / somewhere perhaps?
    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
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    sorry but errors has been #defined as a directory leading to the 404.html file e.g.
    #define errors "/home/.../errors/"

    when i printed the file path it was correct but i just dont know why it didnt open the value of fd1 changed from -1 to 5 or 6 cant remember at the moment but i do know that means it was successful

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Yes. for the open(), a returned -1 means unsuccessful. Other numbers are OK.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    so why did it not appear on the web browser... this is why im getting confused

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I'm saying the file open must have failed before the if(fd1 == -1)... then when you do the open, that one succeeds. At least from the code that I can see that you posted.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try
    Code:
    fd1 = open(resource,O_RDONLY,0);
    if ( fd1 < 0 ) {
        perror("Unable to open file");
    }
    Probable guesses are "file not found" or "permission denied".
    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.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Are you expecting that because you're calling the open() function on a file that has a .html extension it will open in a web browser? If you are, well that's not how it works. You need to use a call to the operating system's shell, like ShellExecute if you're on Windows.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    Ill try the code when i get up in the morning thanks salem
    i am on ubuntu and it had worked in the past on another program
    and yea maybe it does fail before i guess salem's code will sort that out
    thanks for the time and help guys i really appreciate it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM