vs2010报错是“Windows 已在 公交查询系统.exe 中触发一个断点。
其原因可能是堆被损坏,这说明 公交查询系统.exe 中或它所加载的任何 DLL 中有 Bug。
原因也可能是用户在 公交查询系统.exe 具有焦点时按下了 F12。”
代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
long long a;
#define stop system("pause");
#define home(x) printf("%p\n",x);
int j;//控制项目数组
struct item
{
int number;
char*Item;
};
int main(int argc,char *argv[])
{
system("color f9");
system("title 公交查询系统");
FILE *fp;
int i;//线路选择
char c;
c=(char)malloc(sizeof(char));
printf("字符变量的地址是:");
home(c);
int amount=0;//交通线路的数量
fp=fopen("file.txt","rb+");
if(fp==NULL)
{
printf("获取失败╮(╯▽╰)╭\n");
system("pause");
return 0;
}
printf("文件指针的地址是:%p\n",fp);//确保文件指针分配到了内存
fseek(fp,0,SEEK_END);
int file_size=ftell(fp);//计算文件的大小
fseek(fp,0,SEEK_SET);
char *str=NULL;//不知道有没有分配内存
str=(char*)malloc(sizeof(*fp));//分配一个文件大小的内存给指针
printf("指针str的地址是:");
home(str);//用C语言宏 确认str指针分配到了地址
str[file_size]='\0';//用空字符结尾 防止字符串乱码
fread((void*)str,file_size,1,fp);//把文件内容读给str
printf("文件大小:%d\n",file_size);//确认文件大小正确 不为零
int seek=0;//设置循环变量
while(seek<file_size)
{
if(str[seek]=='.')//检索项 用数组运算符 遇到'.'分为一项
{
amount++;//amount是项目数
static char*one=NULL;//静态变量 作为str的副本╮(╯▽╰)╭
one=(char*)malloc(sizeof(*fp));
static int one_1=1;
static int seek_1=seek;
while(one_1<=seek)
{
sprintf(one,"%c",str[one_1]);
}
}
seek++;
}
if((c=getc(fp))!=NULL)
{
printf("读入到文件尾!\n");
}
puts(str);//检查文件内容是否不存在
std::cout<<amount<<std::endl;
fclose(fp);
fp=NULL;
struct item ch[5];
{//使自动变量在代码块结尾 退出堆
int i=1;
char*ch_1=NULL;//存储项的内容
ch_1=(char*)malloc(sizeof(char*));
printf("临时字符串的地址:%p\n",ch_1);//确保字符串分配到了地址
stop;//C语言宏 system("pause");
while(i<amount)
{
do{
c=str[a];
sprintf(ch_1,"%c",c,'\0');
}while(a==1);
std::cout<<ch_1<<"\n"<<c<<"\n";//确保临时字符串有内容
ch[j].Item=NULL;
ch[j].Item=(char*)malloc(sizeof(*fp));
printf("项目的地址:%p\n",ch[j].Item);//确保存储项的结构数组中的指针分配到了内存
std::cout<<j<<"\n";//确保下标未越界
stop;
while(ch_1!="。")
{
ch[j].number=i;//此时 第一项 用number标识这是第几项
strcat(ch[j].Item,ch_1);//把ch_1的内容给连接在指针的末尾
if(c==0)
{
std::cout<<"读取失败\n";
stop;
std::cout<<ch[j].Item<<"\n";
stop;
}
c=str[a];//继续获取文件内容
sprintf(ch_1,"%c",c);
a++;
}
strcat(ch[j].Item,"。");
j++;
i++;
}
free(ch_1);
}
printf("1.数据维护(增加项、删除项)\n2.查询\n3.退出\n");
scanf("%d",&i);
switch(i)
{
case 1:
int a;
printf("你真的做决定了吗?如果你确定的话请选择:\n1.增加项\n2.退出\n");
scanf("%d",&a);
if(a==1)
{
fp=fopen("file.txt","w+");//以读模式来覆盖文本 方便添加项目
char*add_1=(char*)malloc(sizeof(*fp));
sprintf(add_1,"%d.",++amount);//add_1存储文件的序号 如当前有5项
char*add_2=(char*)malloc(sizeof(*fp));//add_2是要添加的文本
printf("在此处添上你要添加的文本\n");
scanf("%s",add_2);
strcat(add_1,add_2);
strcat(add_1,"。");
strcat(str,add_1);//把文本添加到末尾
puts(str);
stop;
fprintf(fp,"\n");
fprintf(fp,str);//确认添加成功
fclose(fp);
fp=NULL;
add_1=add_2=NULL;
free(add_1);
free(add_2);
}
else if(a==2)
{
fp=fopen("file.txt","w+");
int d;
printf("请选择你要删除第几项\n");
scanf("%d",&d);
ch[d].Item="";
str="";
for(j=1;;j++)
{
strcat(str,ch[j].Item);
}
fprintf(fp,str);
fclose(fp);
fp=NULL;
for(seek=0;seek<amount;seek++)
{
free(ch[seek].Item);
}
}
break;
case 2:
puts(str);
break;
case 3:
system("pause");
return 0;
break;
default:
break;
}
str=NULL;
free(str);
system("pause");
return 0;
}
运行结果在附属的图片中
还请帮忙看看是什么地方出了错误..