473,412 Members | 4,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

best way to chop off leading char in string?

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[i];

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

Thanks,
Aaron

Nov 13 '05 #1
3 4977

"Aaron Walker" <ka*****@REMOVETHIScfl.rr.com> wrote in message
news:GJ*******************@twister.tampabay.rr.com ...
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[i];

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
Nov 13 '05 #2
On Sat, 22 Nov 2003 19:24:22 +0000, Aaron Walker wrote:
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[i];


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.

Nov 13 '05 #3
On Sat, 22 Nov 2003 19:24:22 +0000, Aaron Walker wrote:
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[i];

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.

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Chris Connett | last post by:
I was wondering if there were a well known pattern/idiom for breaking a sequence into a list of lists, each n elements long, e.g. -> ,,] This comes up reasonably often in my work, and every...
3
by: yanir | last post by:
Hi I use reponse.contenttype = "application/vnd.ms-excel" So the browser will show the data in excel format, but for some fields I use leading zero's, which truncated by the browser, at this...
6
by: david | last post by:
Hi, I have an application as follows: MySQL database Back-Eend linked to MS Access Front-End and ASP Web Application. I require users to enter Serial Numbers such as: 0105123567 (10...
2
by: Myk | last post by:
Hello All, None of the solutions I have found in the archives seem to solve my problem. I have a date column in my tables (stored as a char(10)) which I would like to append a leading zero to...
6
by: Generic Usenet Account | last post by:
I have worked out the following implementation for trimming the leading and trailing whitespace characters of a string (I am surprised not to see these as member functions). Am I doing it...
8
by: rayw | last post by:
Following on from my post yesterday about nums -> binary strings ... I've written two routines that can trim the leading zeros from the results, so I call either of these like this: char *...
11
by: ram23 | last post by:
Hi friends, i need help .. i am getting String value and i need to add leading zeros.. total length of Target string is 13char eg: suppose if i get 12 as values.. it needs to convert into...
4
by: bobm2005 | last post by:
Whatever format I try in Printf, an 'E' format number nearly always has a leading non-zero:- 1.2345E7 -9.3456E8 etc. Is it possible to force it (printf) always to have leading zero? ...
0
by: Monty | last post by:
Hi All, I am having a problem with leading zeros being stripped from fields in a CSV file when I bring them in using Jet/OleDB. In VB.Net/VS 2008, I am accessing a CSV file like so: sSQL =...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.