473,324 Members | 2,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Problem with strncpy

osfreak
22
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(uFilename field) were not copied properly??
Jan 21 '09 #1

✓ answered by JosAH

@osfreak
You aren't copying a string so don't use any str* function. Use memcpy() or memmove() instead.

kind regards,

Jos

5 2271
newb16
687 512MB
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
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
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 Expert 8TB
@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
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
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...
12
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...
2
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
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...
17
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...
2
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...
27
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...
4
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...
6
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...
2
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....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.