char s[200];
char strdeal[200];
char area[10];
char name[100];
int i=0;
ifstream ifs("C://Documents and Settings//Administrator//桌面//work//处理样本//产业园区.txt",ios_base::binary);
ofstream ofs("C://Documents and Settings//Administrator//桌面//work//输出样本//产业园区deal.txt",ios_base::binary);
while(ifs.good())
{
ifs.getline(s,200,'/n');
DealStrWitht(s);
if(!IsContains(s,"7天")&&(IsContains(s,"类别")||IsContains(s,"/"")||IsContains(s,"一篇")||IsContainsNumber(s)))
continue;
cout<<s<<endl;
if(i%2 == 0)
{
if(Isblank(s))
{
i--;
continue;
}
strcpy(strdeal,s);
}
else
{
if(Isblank(s))
{
i++;
continue;
}
// char area[10];//定义在这里面会出错
// char name[100];//
strcpy(name,strdeal);
GetArea(s,area);
strcpy(strdeal,area);
strcat(strdeal,name);
strcat(strdeal,"/t");
strcat(strdeal,s);
strcat(strdeal,"产业园区/n");
ofs.write(strdeal,strlen(strdeal));
}
i++;
assert(s);
}
ifs.close();
ofs.close();
注释的那两行,如果这两个在这里定义的话就会ofs.write(strdeal,strlen(strdeal));
的时候内存非法访问
这是什么原因?仍未解决,跪求原因
//用到的函数
void DealStrWitht(char *str)
{
char *s=str;
char *addstr="/t";
while(*s != ' ' && *s != '/0')
{
s++;
}
*s++='/t';
*s='/0';
}
void GetArea(char *strSource,char *area)
{
char *ss=strSource;
char *sa=area;
char *scity="市";
bool findcity=false;
int i=0;
while(*ss != '/0')
{
if(*ss == *scity && *(ss+1) == *(scity+1))
{
findcity=true;
ss++;
ss++;
strcpy(sa,ss);
*(sa+6)='/0';
strcat(sa,"/t");
break;
}
ss++;
}
}
bool IsContains(char *strSource,char *strDec)
{
char *ss=strSource;
char *sd=strDec;
while(*ss!='/0')
{
if(*ss == *sd)
{
char *sstemp=ss;
while(*sd != '/0' && *sstemp == *sd)
{
sd++;
sstemp++;
}
if(*sd =='/0')return true;
}
ss++;
}
return false;
}
bool IsContainsNumber(const char *strSource)
{
if(*strSource <= '9' && *strSource >='0')return true;
if(*strSource == '一')return true;
return false;
}
bool Isblank(char *strSource)
{
char *ss=strSource;
if(*ss == '/n')return true;
while(*ss !='/0'&& *ss++ ==' ')
{
}
if(*ss-- == '/0')return true;
if(*ss == '/n')return true;
return false;
}