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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange characters in a string (char[20])

Status
Not open for further replies.

cadbilbao

Programmer
Apr 9, 2001
233
ES
Hi again !!!

Using VC++, I want to extract a substring (name) from another
string (infile):


-------------//----------------------
void dodo(char *infile) {

int lgt;
lgt = strlen(infile);

char name[10];

for (int x=0; x<=lgt-5; x++)
name[x]=infile[x];

printf (&quot;name=%s&quot;,name);

...

-------//----------------


But what I get is this:
name = JohnÌÌÌÌÌ

How can I remove those strange characters???

TIA.
 
Hi

What is wrong is you are missing the end of string character from the name string. What you can do is after the for loop put

name[x] = '\0'; // terminate the string

Hope that works.

 
lgt = strlen(infile);
lgt in this case will be the number of characters excluding the null of the end. You need lgt = strlen(infile)+1; John Fill
1c.bmp


ivfmd@mail.md
 
Well, it looks like &quot;infile&quot; does not have a '\0' at the end of it. Do you get the same output if you do the printf with infile? What happens with a strcpy? and what do you get for the strlen? This looks like a very specific case where you know the length of the string.

I think this will add the '\0' automatically

strncpy(name,infile,lgt-5);

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top