4

I am writing some code that uses the function gethostbyname(). This function requires that I pass it a string of the host I am trying to find the host for. Right now I have my string declared in an array of characters, with a null byte at the end so that it is considered a string.

When I do a printf like this:printf("\n%s\n",hostName); the code will print correctly and say something like: facebook.com

However when I try to print the string like this: printf("\n%sX\n",hostName); the output will be Xacebook.com for some reason.

Does anyone know why the X would overwrite the first character of my string? I would think that it should print like "facebook.comX".

user494216
  • 522
  • 4
  • 7
  • 13
  • Yeah it is being read from there. Do you know how to get rid of the \r so we can supply it as an argument to gethostbyname correctly? –  May 19 '11 at 05:23

1 Answers1

4

You have a \r at the end of the string. That moves the cursor back to the start of the line. I'm guessing that you are reading in the hostname from a file?

Laurion Burchall
  • 2,843
  • 16
  • 12