- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: strncpy's problem
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2001 02:35 PM
01-10-2001 02:35 PM
strncpy's problem
#include
#include
main(){
char str1[50]="aaaaaaaaaa", str2[50]="bbbbbbbbbbb", str3[50]="ccccccccccccc",str4[50];
strncpy(str4,str2,8); printf("str4 is %s \n", str4);
strncpy(str4,str3,4); printf("str4 is %s \n", str4);
strncpy(str4,str1,8); printf("str4 is %s \n", str4);
strncpy(str4,str3,4); printf("str4 is %s \n", str4);
}
and got following output:
str4 is bbbbbbbb
str4 is ccccbbbb
str4 is aaaaaaaa
str4 is ccccaaaa
However, I acutally want to get output like this:
tstr4 is bbbbbbbb
str4 is cccc
str4 is aaaaaaaa
str4 is cccc
What should I do to get the output I want?
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2001 08:42 PM
01-10-2001 08:42 PM
Re: strncpy's problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2001 11:32 PM
01-10-2001 11:32 PM
Re: strncpy's problem
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.
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.
Best regards,
Dan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2001 11:40 PM
01-10-2001 11:40 PM
Re: strncpy's problem
Instead of strncpy(), you could use sprintf() to copy your data across strings.
This will allow formatting of the data.
Example:
#include
#include
main()
{
char a[20]="aaaaaaaaaaaaaaa";
char b[20]="bbbbbbbbbbbbbbb";
sprintf(b,"%4.4s",a);
printf("b is %s\n",b);
}
This prints 'b is aaaa' as it copies exactly 4 bytes.
See 'man sprintf' for information about all formatting capabilities.
Best regards,
Dan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2001 02:11 AM
01-11-2001 02:11 AM
Re: strncpy's problem
you could make a procedure that adds the NULL termination ie:
void mystrncpy(dest, src, len)
char *dest;
char *src;
int len;
{
strncpy(dest, src, len);
dest[len] = 0;
}
Regards