NO

Author Topic: printf a string is adding characters !?!  (Read 2952 times)

jakeb211

  • Guest
printf a string is adding characters !?!
« on: January 30, 2010, 09:36:26 PM »
Hello,  I am having a little bit of a problem.  I posted a question a little while ago about passing strings back from functions, and found this forum very hospitable to a newbie, so i'm back with another question.

My function to request the user's information is:

void edt_usr_init(char *finit, char *minit, char *linit)
{   
   /* Clear the screen */
   system ("cls");

      /* Prompt for and recieve user initials */
      printf ("Enter User's initials (xxx) >>\n");
      scanf (" %c%c%c", finit, minit, linit);

}


My function to write to a file, for practice purposes, is:
(I'm using this to check the values that will eventually all be contained is strings, but these results weren't expected. A call to printf first made me aware of it.)

void usr_info(void)
{   
   char   ini[3];
   int    kg;
   FILE   *outp;   /* pointer to output file */

      outp = fopen("practice/usr_stat.txt", "a"); /* open the output file to append it */
      
      edt_usr_init(&ini[0], &ini[1], &ini[2]);   /* get the user's info */
      edt_usr_wght(&kg);                         /* get the user's weight */

      fprintf(outp, "\n%s\n%d", ini, kg);      /* print output to file */

      fclose(outp);                                  /* close the output file */
      
}

for an input of "jab" for usr_init fuction and "104" for usr_wght, my text file ends up reading...

jab€ÿ
104

I don't quite get it.  If I make the string longer assuming that "\0" will be placed at the end of data input, which is what i thought i was doing by making the string length [3], it just changes the funny characters after my user input.

If anyone has any thoughts, I'd be grateful. Thank you.

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: printf a string is adding characters !?!
« Reply #1 on: January 31, 2010, 10:58:48 PM »
You are reading in three single characters but use printf to print a sting, which has to be terminated by a 0x00 character. But you are not providing such a 'zero termination', hence printf when pointed to a string, just keeps printing characters until it 'accidently' encounters a zero byte...
Simplest workaround is to expand ini to contain 4 characters (ini[4]) and initialize ini [3] fixed with 0x00...

Ralf

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: printf a string is adding characters !?!
« Reply #2 on: February 01, 2010, 07:52:26 AM »
.. or you can change the printf-function:
Code: [Select]
fprintf(outp, "\n%c%c%c\n%d", ini[0], ini[1], ini[2], kg);      /* print output to file */
.. but I would prefer the solution of Ralf.  ;)
best regards
 Alex ;)

jakeb211

  • Guest
Re: printf a string is adding characters !?!
« Reply #3 on: February 04, 2010, 02:41:28 AM »
Thank you for the responses, I actually just realized that and was coming back here to post the answer.  You were exactly right there was no '\0' at the end of the chars.  Thanks again!