<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: strncpy's problem in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/strncpy-s-problem/m-p/2481500#M798752</link>
    <description>I haven't got a whole lot of experience with C, but I would say you should probably set your str4 to a null in between copies.  I think that will then give you the results you are looking for.</description>
    <pubDate>Thu, 11 Jan 2001 04:42:45 GMT</pubDate>
    <dc:creator>Patrick Wallek</dc:creator>
    <dc:date>2001-01-11T04:42:45Z</dc:date>
    <item>
      <title>strncpy's problem</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/strncpy-s-problem/m-p/2481499#M798751</link>
      <description>I ran the following c code:&lt;BR /&gt;&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;#include &lt;STRING.H&gt;&lt;BR /&gt;main(){&lt;BR /&gt;  char str1[50]="aaaaaaaaaa", str2[50]="bbbbbbbbbbb", str3[50]="ccccccccccccc",str4[50];&lt;BR /&gt;&lt;BR /&gt;  strncpy(str4,str2,8); printf("str4 is %s \n", str4);&lt;BR /&gt;  strncpy(str4,str3,4); printf("str4 is %s \n", str4);&lt;BR /&gt;  strncpy(str4,str1,8); printf("str4 is %s \n", str4);&lt;BR /&gt;  strncpy(str4,str3,4); printf("str4 is %s \n", str4);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;and got following output:&lt;BR /&gt;str4 is bbbbbbbb&lt;BR /&gt;str4 is ccccbbbb&lt;BR /&gt;str4 is aaaaaaaa&lt;BR /&gt;str4 is ccccaaaa&lt;BR /&gt;&lt;BR /&gt;However, I acutally want to get output like this:&lt;BR /&gt;tstr4 is bbbbbbbb&lt;BR /&gt;str4 is cccc&lt;BR /&gt;str4 is aaaaaaaa&lt;BR /&gt;str4 is cccc &lt;BR /&gt;&lt;BR /&gt;What should I do to get the output I want?&lt;BR /&gt;&lt;BR /&gt;Thanks!&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/STRING.H&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Wed, 10 Jan 2001 22:35:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/strncpy-s-problem/m-p/2481499#M798751</guid>
      <dc:creator>Xiaoming Zhang</dc:creator>
      <dc:date>2001-01-10T22:35:25Z</dc:date>
    </item>
    <item>
      <title>Re: strncpy's problem</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/strncpy-s-problem/m-p/2481500#M798752</link>
      <description>I haven't got a whole lot of experience with C, but I would say you should probably set your str4 to a null in between copies.  I think that will then give you the results you are looking for.</description>
      <pubDate>Thu, 11 Jan 2001 04:42:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/strncpy-s-problem/m-p/2481500#M798752</guid>
      <dc:creator>Patrick Wallek</dc:creator>
      <dc:date>2001-01-11T04:42:45Z</dc:date>
    </item>
    <item>
      <title>Re: strncpy's problem</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/strncpy-s-problem/m-p/2481501#M798753</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;This is the right behavior for 'strncpy(s1,s2,n)' as it will copy exactly n bytes. The result will not be automatically null terminated if if the length of s2 is n or more.&lt;BR /&gt;&lt;BR /&gt;After strncpy(str4,str3,4), you should set str[4] to null to properly terminate your string so that the printf statement will find the end of string where you want it to be.&lt;BR /&gt;&lt;BR /&gt;Best regards,&lt;BR /&gt;&lt;BR /&gt;Dan&lt;BR /&gt;</description>
      <pubDate>Thu, 11 Jan 2001 07:32:01 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/strncpy-s-problem/m-p/2481501#M798753</guid>
      <dc:creator>Dan Hetzel</dc:creator>
      <dc:date>2001-01-11T07:32:01Z</dc:date>
    </item>
    <item>
      <title>Re: strncpy's problem</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/strncpy-s-problem/m-p/2481502#M798754</link>
      <description>Hi again,&lt;BR /&gt;&lt;BR /&gt;Instead of strncpy(), you could use sprintf() to copy your data across strings.&lt;BR /&gt;This will allow formatting of the data.&lt;BR /&gt;&lt;BR /&gt;Example:&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;#include &lt;STRING.H&gt;&lt;BR /&gt;&lt;BR /&gt;main()&lt;BR /&gt;{&lt;BR /&gt;char a[20]="aaaaaaaaaaaaaaa";&lt;BR /&gt;char b[20]="bbbbbbbbbbbbbbb";&lt;BR /&gt;&lt;BR /&gt;sprintf(b,"%4.4s",a);&lt;BR /&gt;printf("b is %s\n",b);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;This prints 'b is aaaa' as it copies exactly 4 bytes.&lt;BR /&gt;&lt;BR /&gt;See 'man sprintf' for information about all formatting capabilities.&lt;BR /&gt;&lt;BR /&gt;Best regards,&lt;BR /&gt;&lt;BR /&gt;Dan&lt;BR /&gt;&lt;/STRING.H&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Thu, 11 Jan 2001 07:40:37 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/strncpy-s-problem/m-p/2481502#M798754</guid>
      <dc:creator>Dan Hetzel</dc:creator>
      <dc:date>2001-01-11T07:40:37Z</dc:date>
    </item>
    <item>
      <title>Re: strncpy's problem</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/strncpy-s-problem/m-p/2481503#M798755</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;you could make a procedure that adds the NULL termination ie:&lt;BR /&gt;&lt;BR /&gt;void mystrncpy(dest, src, len)&lt;BR /&gt;char *dest;&lt;BR /&gt;char *src;&lt;BR /&gt;int   len;&lt;BR /&gt;{&lt;BR /&gt;  strncpy(dest, src, len);&lt;BR /&gt;  dest[len] = 0;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;Regards</description>
      <pubDate>Thu, 11 Jan 2001 10:11:13 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/strncpy-s-problem/m-p/2481503#M798755</guid>
      <dc:creator>Andreas Voss</dc:creator>
      <dc:date>2001-01-11T10:11:13Z</dc:date>
    </item>
  </channel>
</rss>

