Problems with sprintf

CODE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//(...)

char* metod=strtok(request," ");
char* object=strtok(NULL," ");
char* protocol=strtok(NULL,"\n");
object=strtok(object,"/");

char response[1000];

const char* response_404 = "404 Not Found\n";
const char* html_response_404 = "<html><head>\n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\n<p>The requested URL was not found on this server.</p>\n</body><html>\n";

sprintf(response,"%s %s\n%s",protocol,response_404,html_response_404);
cout << response << endl;

OUT:

404 Not Found

<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
</body><html>

SHOULD BE:

HTTP/1.1 404 Not Found

<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
</body><html>


What am I doing wrong?
Last edited on
Please use [code][/code] tags
You are missing the opening double-quotes sign:
sprintf(response,%s %s\n%s",protocol,response_404,html_response_404);

response is not big enough
Last edited on
it's still not working
Can you post the code you have for _404 strings declaration?
The code above should work fine
Here it is (edited above).
That code should work. The only issue I see is that the names don't match but that seems only a copy-paste error
Is protocol ever changing the address it's pointing to?
If I do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
const char* protocol2;

      if (strcmp(protocol,"HTTP/1.0"))
	{
	  protocol2 = "HTTP/1.0";
	}
      if (strcmp(version,"HTTP/1.1"))
	{
	  protocol2 = "HTTP/1.1";
	}

sprintf(response,"%s %s\n%s",protocol2,response_404,html_response_404);
cout << response << endl;


It works! But I don't know why...
Topic archived. No new replies allowed.