473,796 Members | 2,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with strncpy

osfreak
22 New Member
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. typedef struct mystruct {
  5.  
  6.     unsigned char uDirFlag;
  7.  
  8.     unsigned int uFileSize;
  9.  
  10.     char uFilename[255];
  11. } teststruct;
  12.  
  13.  
  14. int main()
  15. {
  16.     char *flatbuf;
  17.  
  18.     teststruct *a = (teststruct*)malloc(sizeof(teststruct));
  19.  
  20.     a->uDirFlag = '0';
  21.     a->uFileSize = 55;
  22.  
  23.     strcpy(a->uFilename,"Test message");
  24.  
  25.     int k = sizeof(teststruct);
  26.  
  27.     flatbuf = (char*)malloc(k);
  28.  
  29.     memset(flatbuf,0,k);
  30.  
  31.     strncpy(flatbuf, (char *)a, k);       //strncpy fails to copy uFilename properly
  32.  
  33.     teststruct *b = (teststruct*)flatbuf;      // b has uFilename field all set to 0
  34.  
  35.     return 0;
  36.  
  37. }
  38.  
  39.  
b has its uFilename field set to 0.

Can anyone Pls explain me why the the character values in the struct(uFilenam e field) were not copied properly??
Jan 21 '09 #1
5 2288
newb16
687 Contributor
Becase unsigned int uFileSize has at least two bytes, and all except least significant are zero, and strncpy stops on it.
Jan 21 '09 #2
osfreak
22 New Member
Is there a way i can force strncpy to copy till the specified bytes?

or is there any other string copy function to do such an operation?
Jan 21 '09 #3
osfreak
22 New Member
I have got it working by using memcpy(),

If there is someother way pls let me know...

Thanks for your help :)
Jan 21 '09 #4
JosAH
11,448 Recognized Expert MVP
@osfreak
You aren't copying a string so don't use any str* function. Use memcpy() or memmove() instead.

kind regards,

Jos
Jan 21 '09 #5
osfreak
22 New Member
Got it working... Thanks anyway...
Jan 21 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1034
by: Cyron | last post by:
Hello All, I am confused with some of the results I am getting from my C++ program. I want the program to display "This is a test" but instead "This is a" is displayed. From experimentation I've found that by commenting out Statement A or Statement B, I obtain the results I want -- but I don't understand why it doesn't work the way I have it setup now. I can see the strncpy() function is working as I expected but somehow something is...
12
15699
by: ­m½Z | last post by:
I am a C programming beginner... I wonder, why strncpy(s, t, n) does not put '\0' at the end of the string. Because when I output the copied string, it output more than what I want, until I put '\0' at the end by myself. But, sometime I don't need put '\0' and it work well?? Like strncpy(s, t, n); strcat(s, t1); .....
2
414
by: Ram Laxman | last post by:
Hi all, I have written the following code: /* strtok example */ #include <stdio.h> #include <string.h> static const char * const resultFileName = "param.txt";
9
2038
by: fybar | last post by:
Hi, I am in the progress of writing a program that will read in the contents of a directory and then do some stuff to some of the files. So far I am getting the contents of the directory but I get a strange result from one file. Here is my program, feel free to critique: #include <sys/types.h> #include <dirent.h> #include <stdio.h>
17
2810
by: G Patel | last post by:
E. Robert Tisdale wrote: > > int main(int argc, char* argv) { > quad_t m = {0, 1, 2, 3}; > int r; > fprintf(stdout, "m = ("); > for (size_t j = 0; j < 4; ++j) Why did you declare j as type size_t ?
2
4455
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c */ #include <time.h>
27
2299
by: smnoff | last post by:
How does( or should user use) strncpy() to allocate enough memory space for the destination string, s1? I guess I am having trouble using strncpy as it seems to be giving me errors. And just as well, I don't know the value of n until the program runs, so it needs to be dynamically allocated. Then again I hard coded n to be number like3 just to even see if that works.
4
9539
by: chikito.chikito | last post by:
1. Can someone tell me the difference between these two functions: void strcpy(char *s1, const char *s2) { while(*s1++ = *s2++) ; } //function prototype of strcpy follows char *strcpy(char *s1, const char *s2) // library function
6
3531
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features to an old program that I wrote in delphi and it's a good opportunity to start with c++.
2
1725
by: James Salisbury | last post by:
Hi, Does anyone have a good page and some exercises on the use of pointers? The compiler is Source Boost and the target is an 18f picr. I am trying to find out why the code below won't compile. At line 244 I get invalid operand and failed to generate expression. Thanks
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9531
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10018
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.