The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
problem with dd command or maybe AFS problem Anta Shell Programming and Scripting 0 08-25-2006 07:10 AM
memcpy segfaults, but not in windows khoma High Level Programming 3 01-12-2006 05:16 PM
SSH Problem auth problem budrito UNIX for Advanced & Expert Users 1 03-17-2004 06:12 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-27-2008
Registered User
 

Join Date: Oct 2004
Posts: 215
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-28-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,491
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);
}
Reply With Quote
  #3 (permalink)  
Old 06-29-2008
Registered User
 

Join Date: Oct 2004
Posts: 215
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
Reply With Quote
  #4 (permalink)  
Old 06-30-2008
Registered User
 

Join Date: May 2008
Posts: 7
Quote:
Originally Posted by arunkumar_mca View Post
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 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 );
Reply With Quote
  #5 (permalink)  
Old 07-01-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 500
Post 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.
Post the source code you are using to accomplish this.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0