为什么这个程序的getNextChar函数无法正确获取下一字符解决方法
为什么这个程序的getNextChar函数无法正确获取下一字符
static fstream iFile;
static fstream oFile;
static const int DONE=8;
static const int bufLen=256;
static const int maxTokenLen=40;
static const int maxReserved=18;
static char lineBuf[bufLen];
static int linePos=0;
static int bufSize=0;
static int bufLine=0;
int getNextChar()
{
if(!(linePos<bufSize))
{
bufLine++;
if(!iFile.eof())
{
iFile.getline(lineBuf,bufLen);
bufSize =strlen(lineBuf);
linePos=0;
return lineBuf[linePos++];
}
else return EOF;
}
else return lineBuf[linePos++];
};
tokenType getToken(){
tokenType current;
int stringIndex=0;
char tokenString[maxTokenLen];
int state=0;
int newstate;
int c=getNextChar();
input ch=getInputType(c);
while(state!=DONE)
{
newstate=T[state][ch];
if(save[state][ch])
{
if(stringIndex<=maxTokenLen)tokenString[stringIndex++]=c;
if(advanced[state][ch])
{
c=getNextChar();
ch=getInputType(c);
}
}
else ungetNextChar();
state=newstate;
};
tokenString[stringIndex]='\0';
current=getTokenType(tokenString);
printToken(current,tokenString);
return current;
};
#include"Scan.h"
int main(int argc, char* argv[]){
if(argc!=2)
{
cout<<"Error!File not exist!";
exit(1);
}
iFile.open(argv[1],ios::in);
oFile.open("result.txt",ios::out);
while(getToken()!=ENDFILE);
iFile.close();
oFile.close();
return 0;
}
由于字数限制之复制了部分代码,为什么如果在主函数直接调用getNextChar就可以获取,如果通过getToken调用就无法获取。
还有如果选debug模式调试会报错,如下:
1>------ 已启动全部重新生成: 项目: Scanner_v0.3, 配置: Debug Win32 ------
1>正在删除项目“Scanner_v0.3”(配置“Debug|Win32”)的中间文件和输出文件
1>正在编译...
1>cl: 命令行 error D8016 :“/O2”和“/RTC1”命令行选项不兼容
1>项目 : error PRJ0002 : 错误的结果 2 (从“e:\开发工具安装\VC\bin\cl.exe”返回)。
1>生成日志保存在“file://d:\Users\Lin\Documents\Visual Studio 2008\Projects\Scanner_v0.3\Scanner_v0.3\Debug\BuildLog.htm”
1>Scanner_v0.3 - 1 个错误,0 个警告
========== 全部重新生成: 成功 0 个,失败 1 个,跳过 0 个 ==========
------解决方案--------------------
char tokenString[maxTokenLen];
根据以上定义,以下
if(stringIndex<=maxTokenLen)tokenString[stringIndex++]=c;
将越界.应改为:
if(stringIndex<maxTokenLen-1)tokenString[stringIndex++]=c;
static fstream iFile;
static fstream oFile;
static const int DONE=8;
static const int bufLen=256;
static const int maxTokenLen=40;
static const int maxReserved=18;
static char lineBuf[bufLen];
static int linePos=0;
static int bufSize=0;
static int bufLine=0;
int getNextChar()
{
if(!(linePos<bufSize))
{
bufLine++;
if(!iFile.eof())
{
iFile.getline(lineBuf,bufLen);
bufSize =strlen(lineBuf);
linePos=0;
return lineBuf[linePos++];
}
else return EOF;
}
else return lineBuf[linePos++];
};
tokenType getToken(){
tokenType current;
int stringIndex=0;
char tokenString[maxTokenLen];
int state=0;
int newstate;
int c=getNextChar();
input ch=getInputType(c);
while(state!=DONE)
{
newstate=T[state][ch];
if(save[state][ch])
{
if(stringIndex<=maxTokenLen)tokenString[stringIndex++]=c;
if(advanced[state][ch])
{
c=getNextChar();
ch=getInputType(c);
}
}
else ungetNextChar();
state=newstate;
};
tokenString[stringIndex]='\0';
current=getTokenType(tokenString);
printToken(current,tokenString);
return current;
};
#include"Scan.h"
int main(int argc, char* argv[]){
if(argc!=2)
{
cout<<"Error!File not exist!";
exit(1);
}
iFile.open(argv[1],ios::in);
oFile.open("result.txt",ios::out);
while(getToken()!=ENDFILE);
iFile.close();
oFile.close();
return 0;
}
由于字数限制之复制了部分代码,为什么如果在主函数直接调用getNextChar就可以获取,如果通过getToken调用就无法获取。
还有如果选debug模式调试会报错,如下:
1>------ 已启动全部重新生成: 项目: Scanner_v0.3, 配置: Debug Win32 ------
1>正在删除项目“Scanner_v0.3”(配置“Debug|Win32”)的中间文件和输出文件
1>正在编译...
1>cl: 命令行 error D8016 :“/O2”和“/RTC1”命令行选项不兼容
1>项目 : error PRJ0002 : 错误的结果 2 (从“e:\开发工具安装\VC\bin\cl.exe”返回)。
1>生成日志保存在“file://d:\Users\Lin\Documents\Visual Studio 2008\Projects\Scanner_v0.3\Scanner_v0.3\Debug\BuildLog.htm”
1>Scanner_v0.3 - 1 个错误,0 个警告
========== 全部重新生成: 成功 0 个,失败 1 个,跳过 0 个 ==========
------解决方案--------------------
char tokenString[maxTokenLen];
根据以上定义,以下
if(stringIndex<=maxTokenLen)tokenString[stringIndex++]=c;
将越界.应改为:
if(stringIndex<maxTokenLen-1)tokenString[stringIndex++]=c;