Problem with memcpy


 
Thread Tools Search this Thread
Top Forums Programming Problem with memcpy
# 1  
Old 06-27-2008
Problem with memcpy

Hi ,

I am having records in a file like

00412772784705041008FRUITFUL STRWBRRY
00412772784703041008FRUITFUL STRWBERE
00000570632801448078 X



i have declared a structure like

typedef struct {
char Uname[16];
char Pname[4];
char Sname[17];

} person;

I am using reading the file one line by line and i am doing memcpy to
store the line into the structure

memset(person,0,sizeof(person));
memcpy(person,line1,37) -- here line1 has the line from the file the above three lines i mentioned

The problem is that ,for 1 and 2 line i am getting the correct result , For third line it is storing
withthe space as well.

my Sname for line 3 contains

" X "

but it should be as

"X "

i used trim function on sname like trim(person.Sname). If i do this is is storing as a single character
as "X" ..

But i need the the 17 character string it should be as

"X "

any way can we do this ??..

Please help ..

Thanks in Advance,
Arun
# 2  
Old 06-28-2008
What you are doing is correcting the data, not reading it. You memcpy is correct.
If you must change the data, try this
Code:
if (person.Sname[0]==' ')
{
    char tmp[18]={0x0};
    char *p=person.Sname;
    while(*p==' ') p++;
    sprintf(tmp, "%-17s", p);
    memcpy(person.Sname, 17, tmp);
}

# 3  
Old 06-29-2008
Thanks jim ..

i tried this , it is showing access vilolation error at the memcpy line . I tried with debugging . I can able to find the error only at memcpy line . Can you please help me to correct this.

It is compiling , but the error is comming at runtime

Thanks,
Arun
# 4  
Old 06-30-2008
Quote:
Originally Posted by arunkumar_mca
Thanks jim ..

i tried this , it is showing access vilolation error at the memcpy line . I tried with debugging . I can able to find the error only at memcpy line . Can you please help me to correct this.

It is compiling , but the error is comming at runtime

Thanks,
Arun
Hi,

actualy Jimi made an error SmilieSmilie in his code the sintax from memcpy is like
this

void * memcpy ( void * destination, const void * source, size_t num );

i think you notice the diference.

try to write memcpy(person.Sname, tmp, 17 );
# 5  
Old 07-01-2008
Java Post source code

Are you trying to remove leading blanks and then adding them to the end of the array of characters stored in the struct person. Smilie
Post the source code you are using to accomplish this.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sed Or Grep Problem OR Terminal Problem?

I don't know if you guys get this problem sometimes at Terminal but I had been having this problem since yesterday :( Maybe I overdid the Terminal. Even the codes that used to work doesn't work anymore. Here is what 's happening: * I wanted to remove lines containing digits so I used this... (25 Replies)
Discussion started by: Nexeu
25 Replies

2. Shell Programming and Scripting

Undefined reference to memcpy@GLIBC_2.14

Dear All, I am trying to compile OpenFOAM-1.7.x in RHEL. I could not able to compile some of the applications because of libc version issues. It is saying undefined reference to memcpy@GLIBC_2.14 Can anybody look into it? Thanks & Regards, linuxUser_ (3 Replies)
Discussion started by: linuxUser_
3 Replies

3. Programming

memcpy error

I am getting segmentation fault in memcpy.I have given sufficient memory but i dont know why it is occurring char *finalptr = ( char *)malloc(1048576* sizeof(char)); finaloffset=0;have=685516; memcpy(&(finalptr)+finaloffset,out,have); finaloffset=685516;have=359910;... (23 Replies)
Discussion started by: rajsekhar28
23 Replies

4. IP Networking

Problem with forwarding emails (SPF problem)

Hi, This is rather a question from a "user" than from a sys admin, but I think this forum is apropriate for the question. I have an adress with automatic email forwarding and for some senders (two hietherto), emails are bouncing. This has really created a lot of problems those two time so I... (0 Replies)
Discussion started by: carwe
0 Replies

5. Programming

Segmentation Fault by memcpy

Hello everybody, I'm coding a test program for ARP protocol, and i don't know why i'm getting a SIGSEGV, i traced it with gdb and it says it's due to the memcpy function from /lib/libc.so.6. Program received signal SIGSEGV, Segmentation fault. 0xb7e9e327 in memcpy () from /lib/libc.so.6 This... (5 Replies)
Discussion started by: Zykl0n-B
5 Replies

6. UNIX for Dummies Questions & Answers

DHCP problem and eth1 problem

At work I am trying to get this one Linux machine (let's call it ctesgm07) to behave like another Linux machine that we have (let's call it test007). test007 returns the following version info: cat /etc/debian_version: lenny/sid uname -a: Linux test007 2.6.27-7-generic #1 SMP Tue Nov 4... (0 Replies)
Discussion started by: sllinux
0 Replies

7. AIX

user login problem & Files listing problem.

1) when user login to the server the session got colosed. How will resolve? 2) While firing the command ls -l we are not able to see the any files in the director. but over all view the file system using the command df -g it is showing 91% used. what will be the problem? Thanks in advance. (1 Reply)
Discussion started by: pernasivam
1 Replies

8. Shell Programming and Scripting

ssh script problem problem

Hi Please help me with the following problem with my script. The following block of code is not repeating in the while loop and exiting after searching for first message. input_file ========== host001-01 host001-02 2008-07-23 13:02:04,651 ConnectionFactory - Setting session state... (2 Replies)
Discussion started by: pcjandyala
2 Replies

9. Shell Programming and Scripting

problem with dd command or maybe AFS problem

Hi, folks. Sorry for bothering, but maybe someone could help me please. The problem is the following: there is some script that copies files from local file system to AFS. The copying is performed with dd command. The script copies data into some AFS volumes. The problem appeared with one... (0 Replies)
Discussion started by: Anta
0 Replies

10. Programming

memcpy segfaults, but not in windows

Hi Having a lil trouble with a rather simple application I'm writing. It so happens that I have to copy some data using memcpy() and so far I've been doing just fine compiling it with VC.Net and running it on Windows XP. Now I'm trying to port the thing to Solaris (which shouldn't really be too... (3 Replies)
Discussion started by: khoma
3 Replies
Login or Register to Ask a Question