免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2148 | 回复: 5
打印 上一主题 下一主题

[C] 关于strncpy的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-11 17:59 |只看该作者 |倒序浏览
有这么一个文件
0000.MP3
0001.MP3
0002.MP3
0003.MP3
0004.MP3
0005.MP3


将此文件全部读入内存中
分别使用sscanf读取数字用strncpy读取字符串。
sscanf ( p_cur, "%d", &num );
p_cur = strpbrk ( p_cur,"." ) +1;
strncpy ( str, ( char * ) p_cur,10 );



为什么 str 中的内容变成了
MP3
0001.MP


难道strncpy不是遇到“\n”就返回了吗?

论坛徽章:
1
双子座
日期:2015-01-04 14:25:06
2 [报告]
发表于 2008-09-11 18:02 |只看该作者
谁告诉你 strncpy遇到“\n”就返回的?
去揍他一顿时

论坛徽章:
0
3 [报告]
发表于 2008-09-11 18:11 |只看该作者
原帖由 yecheng_110 于 2008-9-11 18:02 发表
谁告诉你 strncpy遇到“\n”就返回的?
去揍他一顿时

一起揍~~~

strncpy的n是表示复制的最大个数是第三个参数 int n 决定的

论坛徽章:
0
4 [报告]
发表于 2008-09-11 19:42 |只看该作者
原帖由 5毛党党员 于 2008-9-11 18:11 发表

一起揍~~~

strncpy的n是表示复制的最大个数是第三个参数 int n 决定的


你都说了是最大个数了..咋还说是第三个参数决定的呢....它只决定了最大个数...有可能达不到最大个数的...

论坛徽章:
0
5 [报告]
发表于 2008-09-11 19:54 |只看该作者

  1. The strncpy() function copies no more than n bytes from the array pointed
  2. to by the s2 parameter to the array pointed to by the s1 parameter.  Bytes
  3. following a null byte are not copied. When operating on overlapping loca-
  4. tions, the behavior of this function is unreliable. When the location
  5. pointed to by the s2 parameter is an array that is shorter than n bytes,
  6. null bytes are appended to the s1 value until n bytes in all are written.
复制代码

论坛徽章:
0
6 [报告]
发表于 2008-09-12 09:08 |只看该作者
原帖由 cscscheng 于 2008-9-11 19:42 发表


你都说了是最大个数了..咋还说是第三个参数决定的呢....它只决定了最大个数...有可能达不到最大个数的...



。。。。是我没说清楚,还是你没理解?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP