免费注册 查看新帖 |

Chinaunix

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

string字符串 异常退出 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-06-21 09:25 |只看该作者 |倒序浏览
请教各位:我循环使用string 中的copy函数读写数组,当读到数组末尾的时候出现了异常退出,这种情况是由什么引起的,谢谢!

论坛徽章:
0
2 [报告]
发表于 2007-06-21 10:10 |只看该作者
帖代码看看?

论坛徽章:
0
3 [报告]
发表于 2007-06-21 10:28 |只看该作者
size_type copy (charT* s, size_type n,  size_type pos = 0) const;

      Replaces elements in memory with copies of elements from this string. An out_of_range exception will be thrown if pos > size(). The lesser of n and size() - pos elements of this string, starting at position pos are copied into the array pointed to by s. No terminating null is appended to s.

size_type copy(charT* buf, size_type n, size_type pos = 0) const

        Copies at most n characters from *this to a character array. Throws out_of_range if pos > size(). Otherwise, equivalent to copy(begin() + pos, begin() + pos + min(n, size()), buf). Note that this member function does nothing other than copy characters from *this to buf; in particular, it does not terminate buf with a null character.

确认一下
1.  源和目标有没弄错?
2.  目标中是否有足够的存放空间?

[ 本帖最后由 antonym55 于 2007-6-21 10:50 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2007-06-21 12:35 |只看该作者
先谢谢两位兄弟,代码如下,请帮忙看看。file文件存在 并能read数据。
等下我把数组再放大点试试
char fileCon[10000] = {0};
int fd = open("/home/file", O_RDONLY);
int num = read(fd, fileCon, sizeof(fileCon));
char title[400] ={0};
while( true )
        {
                string a = fileCon;
               
                int indexStart = a.find("test.php?id=", 0);
                char buf[400]={0};
                if((indexEnd<0) || (indexStart<0))
                {
                        break;
                }
                a.copy(buf, indexEnd-indexStart, indexStart);


                //get title
                indexStart = a.find(">", indexStart);
                indexEnd = a.find("</a>", indexStart);
                if( 4<(indexEnd - indexStart)&& (indexEnd - indexStart)<50 )
                {
                      a.copy(title, indexEnd-indexStart-1, indexStart+1);
                      cout << buf << endl;
                      cout << title << endl;
                     
                      memset(title, '\0', sizeof(title));
                      memset(buf, '\0', sizeof(buf));
                      memset(fileCon, '\0', sizeof(fileCon));

                      a.copy(fileCon, a.length()-indexEnd, indexEnd);

                }else
                {
                      memset(fileCon, '\0', sizeof(fileCon));
                      a.copy(fileCon, a.length()-indexEnd, indexEnd);

                      memset(title, '\0', sizeof(title));
                      memset(buf, '\0', sizeof(buf));
                }
        }

如果这个文件大运行一会后才会异常退出。
所以调试很难,所以上来问问,谢谢

[ 本帖最后由 h0tr0ck 于 2007-6-21 13:10 编辑 ]
julianzh 该用户已被删除
5 [报告]
发表于 2007-06-21 12:50 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
6 [报告]
发表于 2007-06-21 13:10 |只看该作者
兄弟再看看。。我改了下代码。
谢谢了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP