Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > copy long strings in C

Reply
Thread Tools

copy long strings in C

 
 
Patrick
Guest
Posts: n/a
 
      08-16-2005
Hello

I have the following "easy" problem. I have a string which contains
1000 chars.
Now my task is to cut the string at his 650 position. I tried strcpy,
to copy the first 650 chars into another string but I got a memory
access error. I have heard that i can use this function only to copy
500 chars is that true? What other options do i have?

Best Regards
Patrick

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      08-16-2005
In article < .com>,
Patrick <> wrote:
>I have the following "easy" problem. I have a string which contains
>1000 chars.
>Now my task is to cut the string at his 650 position. I tried strcpy,
>to copy the first 650 chars into another string but I got a memory
>access error. I have heard that i can use this function only to copy
>500 chars is that true? What other options do i have?


No, that is NOT true. strcpy's lower limits are in the gigabyte range
when there are any limits at all. [After a few gigabytes, you
start overflowing 32 bit pointers...]

Note that if you only want to copy the leading part of a string,
strcpy is not the right function to use. I suggest you reexamine
the documentation for functions that have the letter 'n' in their
names.
--
This signature intentionally left... Oh, darn!
 
Reply With Quote
 
 
 
 
Patrick
Guest
Posts: n/a
 
      08-16-2005
I tried it with strncpy but i get again a memory access violation....

 
Reply With Quote
 
Stefan Klemm
Guest
Posts: n/a
 
      08-16-2005
Am Mon, 15 Aug 2005 23:56:01 -0700 schrieb Patrick:

> Hello
>
> I have the following "easy" problem. I have a string which contains
> 1000 chars.
> Now my task is to cut the string at his 650 position.

string[650] = '\0';

> I tried strcpy,
> to copy the first 650 chars into another string but I got a memory
> access error.

man strncpy
Does the second string have enough memory?

> I have heard that i can use this function only to copy
> 500 chars is that true?

The length of a character array is determined by NULL. The limitation is
the free heap space.

> What other options do i have?

Read a good C book.


Best Regards,
Stefan
 
Reply With Quote
 
Krishanu Debnath
Guest
Posts: n/a
 
      08-16-2005

Patrick wrote:
> Hello
>
> I have the following "easy" problem. I have a string which contains
> 1000 chars.
> Now my task is to cut the string at his 650 position. I tried strcpy,
> to copy the first 650 chars into another string but I got a memory
> access error. I have heard that i can use this function only to copy
> 500 chars is that true? What other options do i have?
>
> Best Regards
> Patrick


char *strncpy(char * restrict s1, const char * restrict s2, size_t n)
Make sure that you understand the function correctly.

Krishanu

 
Reply With Quote
 
ambar.shome@gmail.com
Guest
Posts: n/a
 
      08-16-2005
Will you please write how your program exactly looks like?

Patrick wrote:
> I tried it with strncpy but i get again a memory access violation....


 
Reply With Quote
 
Suman
Guest
Posts: n/a
 
      08-16-2005

Patrick wrote:
> I tried it with strncpy but i get again a memory access violation....

1. Quote context.
2. Are you sure you are allocating memory before calling str*cpy()?
And, remember, you need to allocate strlen(whatever) + 1 chars, to make
room for the terminating null character. Also, post the offending code.

 
Reply With Quote
 
Coder
Guest
Posts: n/a
 
      08-16-2005
Patrick wrote:
> Hello
>
> I have the following "easy" problem. I have a string which contains
> 1000 chars.
> Now my task is to cut the string at his 650 position. I tried strcpy,
> to copy the first 650 chars into another string but I got a memory
> access error. I have heard that i can use this function only to copy
> 500 chars is that true? What other options do i have?
>
> Best Regards
> Patrickint


I think the following code solves ur problem...

main()
{
char st1[1000],st2[650];
int i;

for (i = 0; i < 1000; i++)
st1[i] = 'a';

st1[i] = '\0';

strncpy(st2,st1,sizeof(st2));
st2[650] = '\0';

for (i = 0; st2[i] != '\0'; i++)
printf("%d\t%c\n",i,st2[i]);

printf("%d",i);

}

 
Reply With Quote
 
Giannis Papadopoulos
Guest
Posts: n/a
 
      08-16-2005
Coder wrote:
> Patrick wrote:
>
>>Hello
>>
>>I have the following "easy" problem. I have a string which contains
>>1000 chars.
>>Now my task is to cut the string at his 650 position. I tried strcpy,
>>to copy the first 650 chars into another string but I got a memory
>>access error. I have heard that i can use this function only to copy
>>500 chars is that true? What other options do i have?
>>
>>Best Regards
>>Patrickint

>
>
> I think the following code solves ur problem...
>


When you answer, try to be sure that what you are proposing works ok...

> main()
> {


Shoyld always be

int main(void)
{

> char st1[1000],st2[650];
> int i;
>
> for (i = 0; i < 1000; i++)
> st1[i] = 'a';
>
> st1[i] = '\0';


You have already passed the limits of your st1... You try to access
st1[1000] which is wrong and produces an error in a well-written system..

>
> strncpy(st2,st1,sizeof(st2));
> st2[650] = '\0';


Same mistake here. Moreover, strncpy() does copy the ending '\0' so
there is no need for the erroneous st2[650] = '\0';
>
> for (i = 0; st2[i] != '\0'; i++)
> printf("%d\t%c\n",i,st2[i]);
>
> printf("%d",i);
>


And add a
return 0;

> }
>



--
one's freedom stops where other's begin

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
 
Reply With Quote
 
Suman
Guest
Posts: n/a
 
      08-16-2005

Coder wrote:
> Patrick wrote:
> > Hello
> >
> > I have the following "easy" problem. I have a string which contains
> > 1000 chars.
> > Now my task is to cut the string at his 650 position. I tried strcpy,
> > to copy the first 650 chars into another string but I got a memory
> > access error. I have heard that i can use this function only to copy
> > 500 chars is that true? What other options do i have?
> >
> > Best Regards
> > Patrickint

>
> I think the following code solves ur problem...
>

No. It doesn't.

To start with, you need a few basic headers:

#include <stdio.h>
#include <string.h>

> main()


So, what did you say, your code compiled on?
Use

int main()
> {
> char st1[1000],st2[650];
> int i;
>
> for (i = 0; i < 1000; i++)
> st1[i] = 'a';
>
> st1[i] = '\0';


i is now thousand, and accessing out-of-bounds isn't correct.
I warned about *this* thing only in my previous post, please
take the time out to read the whole thread. Please!

>
> strncpy(st2,st1,sizeof(st2));
> st2[650] = '\0';
>
> for (i = 0; st2[i] != '\0'; i++)
> printf("%d\t%c\n",i,st2[i]);


Indentation is most welcome, btw.

> printf("%d",i);
>


Hm. No \n at end of printf(). You sure you know what you are
doing?

Also a little
return 0;
wouldn't hurt.
> }


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having compilation error: no match for call to ‘(const __gnu_cxx::hash<long long int>) (const long long int&)’ veryhotsausage C++ 1 07-04-2008 05:41 PM
long long and long Mathieu Dutour C Programming 4 07-24-2007 11:15 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
unsigned long long int to long double Daniel Rudy C Programming 5 09-20-2005 02:37 AM
Assigning unsigned long to unsigned long long George Marsaglia C Programming 1 07-08-2003 05:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57