这个错误是怎么回事

一枚小菜 2010-10-30 11:57:39
//nestest.cpp
#include "E:\thinking c++ code\stack\nested.h"
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

main(int argc,char** argv)
{
stack textlines;
FILE* file;
char* s;
#define BUFSIZE 100
char buf[BUFSIZE];
for(int i=0;i<argc;i++)
gets(argv[i]);
assert(argc==2);
textlines.initialize();
file=fopen(argv[1],"r");
assert(file);
while(fgets(buf,BUFSIZE,file))
{
char* string=(char*)malloc(strlen(buf)+1);
assert(string);
strcpy(string,buf);
textlines.push(string);
}

while((s=(char*)textlines.pop())!=0)
{
printf("%s",s);
free(s);
}
textlines.cleanup();
}

进入命令行,输入nestest.cpp nestest.cpp
结果assertion failed,argc==2,file E:\thinking c++ code\stack\nestest.cpp line 16
这时怎么回事,请高手指导!
...全文
110 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
dengqibin 2010-10-30
  • 打赏
  • 举报
回复
你运行程序的命令一定没传两个参数,应该用类似:程序名(.exe) 参数A 参数B 来执行吧
無_1024 2010-10-30
  • 打赏
  • 举报
回复
assert(argc==2);
问题是出在这儿 没有完全的程序没法改
ForestDB 2010-10-30
  • 打赏
  • 举报
回复
程序想干嘛?简言之就是除了生成的exe之外,执行时还要给个参数。
justkk 2010-10-30
  • 打赏
  • 举报
回复
"进入命令行,输入nestest.cpp nestest.cpp"??
你是如何执行的?命令行参数数目肯定不是2,导致assert(argc==2)断言失败
justkk 2010-10-30
  • 打赏
  • 举报
回复
代码没问题,前面不是说了吗,是你的运行方法有问题
这个程序需要两个参数,你命令行提供的不足..
一枚小菜 2010-10-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hnuqinhuan 的回复:]
assert(argc==2);
问题是出在这儿 没有完全的程序没法改
[/Quote]

#ifndef NESTED_H_
#define NESTED_H_


struct stack{
struct link{
void* data;
link* next;
void initialize(void* Data,link* Next);
}*head;
void initialize();
void push(void* Data);
void* peek();
void* pop();
void cleanup();
};

#endif


#include<stdlib.h>
#include <assert.h>
#include "nested.h"

void stack::link::initialize(void* Data,link* Next){
data=Data;
next=Next;
}

void stack::initialize(){
head=0;
}

void stack::push(void* Data){
link* newlink=(link*)malloc(sizeof(link)); //malloc 的空间需要释放
assert(newlink);
newlink->data=Data;
newlink->next=head;
head->next=newlink;
}

void* stack::peek(){
return head->data;
}


void* stack::pop(){
if(head==0) return 0;
void* result=head->data;
link* oldHead=head;
head=head->next;
free(oldHead);
return result;
}


void stack::cleanup(){
link* cursor=head;
while(head)
{
cursor=head->next;
free(head->data);//
free(head);//
head=cursor;
}
}


#include "E:\thinking c++ code\stack\nested.h"
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

main(int argc,char** argv)
{
stack textlines;
FILE* file;
char* s;
#define BUFSIZE 100
char buf[BUFSIZE];
assert(argc==2);
textlines.initialize();
file=fopen(argv[1],"r");
assert(file);
while(fgets(buf,BUFSIZE,file))
{
char* string=(char*)malloc(strlen(buf)+1);
assert(string);
strcpy(string,buf);
textlines.push(string);
}

while((s=(char*)textlines.pop())!=0)
{
printf("%s",s);
free(s);
}
textlines.cleanup();
}


这时完整的代码,运行时就是我说的错误,命令行中还没有输入就出现断言错误!
怎么回事?
foxfsqyp 2010-10-30
  • 打赏
  • 举报
回复
程序名(.exe) 参数A 这样的方式来调用
for(int i=0;i<argc;i++)
gets(argv[i]);
assert(argc==2);
for没必要吧,命令行的参数会自动放在argv对应的内存中。
harderman 2010-10-30
  • 打赏
  • 举报
回复
你的程序没有贴完全

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧