best way to chop off leading char in string?

A

Aaron Walker

I am attempting to write a http server, and the requested url is always
a string like "/filename.html" for example. I need to strip off the '/'
char before I can attempt to open that file, obviously.

Right now, I have the following code:

/* url is previously declared (of type char *) */
int i, c;
char *url_p;

url_p = (char *) malloc(strlen(url));

for(i = 1, c = 0; i <= strlen(url); i++, c++)
url_p[c] = url;

is this the best way to do this or is there a more efficient way?

Thanks,
Aaron
 
M

Mike Wahler

Aaron Walker said:
I am attempting to write a http server, and the requested url is always
a string like "/filename.html" for example. I need to strip off the '/'
char before I can attempt to open that file, obviously.

Right now, I have the following code:

/* url is previously declared (of type char *) */
int i, c;
char *url_p;

url_p = (char *) malloc(strlen(url));

for(i = 1, c = 0; i <= strlen(url); i++, c++)
url_p[c] = url;

is this the best way to do this or is there a more efficient way?


char input[] = "/filename.html"; /* or 'malloc()' it if you prefer */
interface_func(input + 1);

:)

-Mike
 
K

Kelsey Bjarnason

I am attempting to write a http server, and the requested url is always
a string like "/filename.html" for example. I need to strip off the '/'
char before I can attempt to open that file, obviously.

Right now, I have the following code:

/* url is previously declared (of type char *) */
int i, c;
char *url_p;

url_p = (char *) malloc(strlen(url));

Umm... no. Don't cast malloc. It's unneccessary and can hide bugs.
for(i = 1, c = 0; i <= strlen(url); i++, c++)
url_p[c] = url;


You might want to try memmove. It can cope with overlapping regions, so
you could use the same buffer for the source and the destination. If you
really want a secondary buffer (i.e. leave the original data intact) the
obvious answer would be to simply use strcpy:

char *removeleadingchar( const char *src )
{
char *dst = malloc( strlen( src ) );
strcpy( dst, src + 1 );
return dst;
}

Note you should check for NULLs, check that src is long enough, not NULL,
etc.
 
S

Sheldon Simms

I am attempting to write a http server, and the requested url is always
a string like "/filename.html" for example. I need to strip off the '/'
char before I can attempt to open that file, obviously.

Right now, I have the following code:

/* url is previously declared (of type char *) */
int i, c;
char *url_p;

url_p = (char *) malloc(strlen(url));

for(i = 1, c = 0; i <= strlen(url); i++, c++)
url_p[c] = url;

is this the best way to do this or is there a more efficient way?


If the only thing you want to do with the "stripped" string is
to pass it to fopen() or something like that, then you can just
do:

#include <stdio.h>
...
FILE * url_file;
...
url_file = fopen(url+1, "r");
if (url_file) ... else ...

url+1 is a pointer to the second character in the string, 'f' in your
example above. Obviously you can't do this if you want to alter the
"stripped" string without also altering the original string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top