-
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
-
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
-
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.
-
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?
-
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
-
Yes. for the open(), a returned -1 means unsuccessful. Other numbers are OK.
-
so why did it not appear on the web browser... this is why im getting confused
-
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.
-
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".
-
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.
-
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