Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi all - can anyone explain how i c

Status
Not open for further replies.

JaybOt

Programmer
Apr 18, 2001
101
GB
Hi all - can anyone explain how i could delete a character at the end of a string (char*), namely "\n". I have a function that gets the time ...


void GetTime(char *ti) {
time_t rawtime;
struct tm *timeinfo;
char *timedata;

time ( &rawtime ); /* Get the time */
timeinfo = localtime ( &rawtime );
timedata = asctime(timeinfo);
strcpy(ti,timedata); /* Copy the time string */
}

.. Which works o.k. but returns the time WITH a carrage return - my problem is that i need the string, without it.

Any help would be appreciated,

Regards,
J@yb0t.

"Always know what you say, but don't always say what you know!"
 
Something like
[tt]timedata[strlen(timedata) - 1] = '\0';[/tt]
should work.

//Daniel
 
Thanks Daniel!

Tried that but could not get it to work, ended up just copying all chars in the string but 1 (char by char cpy):

for(j=0; j < strlen(nulstr)-1; j++){
newstr[j] = nulstr[j];


Bit crude, but does the job.

Thanks anyway!

J@yb0t[afro]

&quot;Always know what you say, but don't always say what you know!&quot;
 
hi,
to merge the 2 solutions you can:

strncpy( ti, timedata, strlen(timedata)-1 ) ;
or better
memcpy( ti, timedata, strlen(timedata)-1 ) ;

bye


 
I can't see what is wrong with the first response.
The others don't give the terminating zero, so the results are not really strings.

Just my 2 cents...
 
hi

verstrs you are right, what I have written is wrong. sorry!

bye
 
hows about:
(string)ti.erase((string)ti.end());

(from outside in:) cast ti as a string, and call end() getting an iterator to the end of the string; and then use the strings erase() function on it, by casting ti to a string (again)...

Why reinvent the wheel, there is a string library for a reason.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top