在C中复制长字符串 [英] copy long strings in C

查看:91
本文介绍了在C中复制长字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


我有以下简单的说法问题。我有一个包含

1000个字符的字符串。

现在我的任务是将字符串剪切到650位置。我尝试strcpy,

将前650个字符复制到另一个字符串中,但我有一个内存

访问错误。我听说我只能使用这个功能复制

500个字符是真的吗?我有什么其他选择?


最好的问候

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. 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

推荐答案

在文章< 11 ********************** @ g43g2000cwa.googlegroups .com> ;,

Patrick< ; GR ******** @ yahoo.de>写道:
In article <11**********************@g43g2000cwa.googlegroups .com>,
Patrick <gr********@yahoo.de> wrote:
我有以下简单的说法问题。我有一个包含
1000个字符的字符串。
现在我的任务是将字符串剪切到650的位置。我尝试strcpy,
将前650个字符复制到另一个字符串但我得到了一个内存访问错误。我听说我只能使用这个功能来复制500个字符是真的吗?我还有什么其他选择?
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?




不,这不是真的。当有任何限制时,strcpy'的下限是千兆字节范围

。 [几千兆字节之后,你好了>
开始溢出32位指针...]


注意,如果你只想复制一个字符串的前导部分,

strcpy不适合使用。我建议你重新检查



名字中带有'n'字母的函数的文档。

-

这个签名故意留下......哦,笨蛋!



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!


我用strncpy尝试了但是我再次遇到内存访问违规....

I tried it with strncpy but i get again a memory access violation....




Patrick写道:

Patrick wrote:
你好

我有以下内容;易于"问题。我有一个包含
1000个字符的字符串。
现在我的任务是将字符串剪切到650的位置。我尝试strcpy,
将前650个字符复制到另一个字符串但我得到了一个内存访问错误。我听说我只能使用这个功能来复制500个字符是真的吗?我有什么其他选择?

最好的问候
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. 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 *限制s2,size_t n)

确保你正确理解了这个功能。


Krishanu



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

Krishanu


这篇关于在C中复制长字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆