首页
社区
课程
招聘
向C++大虾们请教
发表于: 2011-1-3 20:44 3953

向C++大虾们请教

2011-1-3 20:44
3953
请大虾们帮我看看下面的程序,我在VC6.0下运行老出错:Debug Assertion Failed!
#include<conio.h>
#include<cstdio>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<iostream>
#include<vector>
#include<list>
#include<string>
#include<fstream>
#include<windows.h>
#include<mmsystem.h>
#include<assert.h>

#pragma comment(lib,"winmm.lib")
#define P1 6
static int m=P1;
static char p[P1+1]={""};
static const int MAXCHAR1=256;
class BMLSearch
{
    int deltal[MAXCHAR1];
    char *patt;
public:
    BMLSearch(char*);
    int BMLS(char *p,char *t,int m,int n);
};
char *NorP()//模式串生成模块
{
        strcpy(p, "hello everyone");
        return p;
}
BMLSearch::BMLSearch(char *p)//移动距离计算
{
        assert(p);
        patt=p;
        int k=0;
        for(k=0;k<MAXCHAR1;k++)
                deltal[k]=m+1;
       
        for(k=0;k<m-1;k++)
                deltal[patt[k]]=m-k;
}
int BMLSearch::BMLS(char *p,char *t,int m,int n){//模式匹配
        assert(t);
        if(m>n){}
        int k=m-1;
        while(k<n){
                int j=m-1;
                int i=k;
                while(j>=0&&t[i]-patt[j])
                {j--;i--;}
                if(j==-1)
                        return i+1;
                k+=deltal[t[k+1]];
        }
}
int main()//计时主模块
{
        DWORD start=timeGetTime();//开始
        FILE *fp;//读取文本
        if((fp=fopen("test.txt","w"))==NULL)
        {
                printf("File open failed!\n");
                exit(0);
        }
        char *t=NULL;
        fgets(t,1024,fp);
        fclose(fp);
        static int n=strlen(t); //读取的字符串长度
        char *p=NorP();                        //模式串
        int m=strlen(p);
        BMLSearch BMLS(char *p,char *t,int m,int n);
        DWORD end=timeGetTime();//结束计时
        DWORD Total=end-start;//计算时间
        FILE *fp1;//记录时间
        char str[256];
        if((fp1=fopen("BMLSTimeLogl.txt","w"))==NULL){
                printf("File open failed!\n");
                exit(0);
        }
        fputs(itoa(Total,str,10),fp1);
        //fputs('\n',fp);
        fclose(fp1);
        return 0;
}

[培训]《安卓高级研修班(网课)》月薪三万计划,掌握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法

上传的附件:
收藏
免费 0
支持
分享
最新回复 (8)
雪    币: 65
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
这个可能是你在DEBUG 模式下编译出现的错误,你可以换个模式编译下,用 Release 模式编译下就好了。
2011-1-3 22:17
0
雪    币: 233
活跃值: (285)
能力值: ( LV12,RANK:270 )
在线值:
发帖
回帖
粉丝
3
char *t=NULL;
fgets(t,1024,fp);

在fgets里面有个断言,要求sting(第一个参数)的指针非空,fgets不会负责为读取内容分配内存的,所以需要你自己先申请好内存,然后将指向申请的内存的指针传进去。
2011-1-3 22:29
0
雪    币: 36
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
同楼上
char *t=NULL;
fgets(t,1024,fp);

fgets函数中有个断言assert(str != NULL),如果第一个参数为空,就会弹出截图的对话框,
附带fgets函数说明:

fgets

function
<cstdio>
char * fgets ( char * str, int num, FILE * stream );
Get string from stream

Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is reached, whichever comes first.
A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str.
A null character is automatically appended in str after the characters read to signal the end of the C string.

Parameters

str
Pointer to an array of chars where the string read is stored.
num
Maximum number of characters to be read (including the final null-character). Usually, the length of the array passed as str is used.
stream
Pointer to a FILE object that identifies the stream where characters are read from.
To read from the standard input, stdin can be used for this parameter.

Return Value

On success, the function returns the same str parameter.
If the End-of-File is encountered and no characters have been read, the contents of str remain unchanged and a null pointer is returned.
If an error occurs, a null pointer is returned.
Use either ferror or feof to check whether an error happened or the End-of-File was reached.
2011-1-4 10:03
0
雪    币: 29
活跃值: (18)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
您的代码中:
[CODE]char *t=NULL;
        fgets(t,1024,fp);
        fclose(fp);[/
CODE]
未为t指针分配空间,也就是t未指向任何内存,被您初始化成为了0,引用此处内存必然会出错。
改正方法:

char t[1024];
fgets(t,1024,fp);
fclose(fp);

楼主,我没有币下载代码也下不了,希望您能给我,谢谢了!
2011-1-5 00:46
0
雪    币: 29
活跃值: (18)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
[QUOTE=LittleChao;911493]您的代码中:
char *t=NULL;
        fgets(t,1024,fp);
        fclose(fp);
未为t指针分配空间,也就是t未指向任何内存,被您初始化成为了0,引用此处内存必然会出错。
改正方法:

char t[1024];
fgets(t,1024,fp);
fclo...[/QUOTE]

悲剧,我么看时间。。。
2011-1-5 00:50
0
雪    币: 48
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
[QUOTE=LittleChao;911493]您的代码中:
char *t=NULL;
        fgets(t,1024,fp);
        fclose(fp);
未为t指针分配空间,也就是t未指向任何内存,被您初始化成为了0,引用此处内存必然会出错。
改正方法:

char t[1024];
fgets(t,1024,fp);
fclo...[/QUOTE]
1024个字节最好还是用new吧,不要在栈中分配
2011-1-7 11:37
0
雪    币: 0
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
不会呵呵继续学习中
2011-1-8 07:11
0
雪    币: 32
活跃值: (16)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
9
观望中.这是来看雪的处女贴了!送给LZ了!
2011-1-8 21:44
0
游客
登录 | 注册 方可回帖
返回
//