Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Strange Problem with strncat

This topic is 5938 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I got a strange problem with strncat. This is my code : [ CPP ] #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <cstring> void delete_space(char *text) { int i; int x = strlen(text); char temp[]=""; for (i = 0; i < x; i++) { if (text != 32) { strncat(temp, &text[i], 1); } } strcpy(text, temp); } void main() { char *active; char *active2; char name2[] = "Music_Path = d:\mp3"; char *token; active = strtok(name2, "="); delete_space(active); printf("\n%s, %d",active, strlen(active)); active = strtok(0, " "); delete_space(active); printf("\n%s, %d",active, strlen(active)); getch(); } [ /CPP ] The Problem is my delete_space(char *text) function. It should eliminate all spaces in a string, but it always terminates the for-loop after 4 runs, no matter which string i pass to the function. Could anybody help me ? Its really getting on my nuts... [edited by - Metzler on July 18, 2002 3:14:34 PM]

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
I think the problem is with char temp[]="";. When u r using strncpy, it assumes that the string has enough additional memory to hold the appended part. The program might work for a while till the routine accesses some invalid/protected location.

a better way would be to replace -
strncat(temp, &text, 1);

with -

and u do not need the stmt -
strcpy(text, temp);

at the end. But u might look for multiple space as well.

Share this post

Link to post
Share on other sites
You can''t use strcpy... that copies to the next NULL. But yes it is your char temp[];

do either
char* temp=new char[1024];
char temp[1024]


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!