疑惑:用string保存扫描到的文件内容,输出只有EOF?

la_feng 2009-03-06 12:23:30
代码比较长,但问题只有一个,希望懂的帮帮忙,我是根据别人的mfc的改过来的,但为什么我那样输出不可以,我想要将其输出和保存到文件中,好像没找到上传附件的,只好贴代码了,乐意帮忙的也可q我348067082传给你

由于提示帖子字数过长,没法贴完代码,好心的q我吧,我隐身在线的
main函数如下:

#include "wordanalyse.h"

int main()
{
FILE * TestFile,* fsave;
char *filename=NULL;
int filelen;
/* printf("please input a file name\n");
cin>>filename;*/
TestFile=fopen("test.txt","r");
if(TestFile==NULL)
cout<<"打开文件出错"<<endl;
if((fsave=fopen("out.txt","w"))==NULL)
printf("Open out.txt ERROR");
fseek(TestFile, 0, SEEK_END);
filelen = ftell(TestFile);
cout<<filelen;
char *linebuf=new char[filelen];
//初始化扫描类
wordanalyse wordAnalyse(TestFile,linebuf,filelen );
//开始扫描
wordAnalyse.beginScan();
// fprintf(fsave,"%s",wordAnalyse.m_path);

//把扫描结果输出
// string m_path=wordAnalyse.m_path;
cout<<wordAnalyse.m_path<<endl;
fclose(TestFile);
fclose(fsave);

return 0;
}
...全文
232 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
sagegz 2009-03-06
  • 打赏
  • 举报
回复
一次发不完可以分几楼发嘛!
友情UP了~!
xuguod20042576 2009-03-06
  • 打赏
  • 举报
回复
关注
lightnut 2009-03-06
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 la_feng 的回复:]
不好意思刚才看漏了,输出有了,确实是那个原因,不过还有一个小问题就是怎么把字符串内容保存到文件里,我这样在输出前加上这个fprintf(fsave,"%s",wordAnalyse.m_path);不行
[/Quote]
fprintf(fsave,"%s",wordAnalyse.m_path.c_str());
yangch_nhcmo 2009-03-06
  • 打赏
  • 举报
回复

fseek(TestFile, 0, SEEK_END); //这行直接将文件指针移到了文件最后
la_feng 2009-03-06
  • 打赏
  • 举报
回复
不好意思刚才看漏了,输出有了,确实是那个原因,不过还有一个小问题就是怎么把字符串内容保存到文件里,我这样在输出前加上这个fprintf(fsave,"%s",wordAnalyse.m_path);不行
la_feng 2009-03-06
  • 打赏
  • 举报
回复
原来是对文件操作不熟悉,那应该怎么改好,不知道有没有相应函数,难道我应该计算完文件长度后先关闭文件再打开,不应该这样吧
zhkefa 2009-03-06
  • 打赏
  • 举报
回复
好多,帮顶。
la_feng 2009-03-06
  • 打赏
  • 举报
回复
已补发,多谢提醒,希望有空关注一下
lightnut 2009-03-06
  • 打赏
  • 举报
回复
我还真猜对了:)
你的wordanalyse确实没有调整传进去的TestFile指针.

fseek(TestFile, 0, SEEK_END); // 这里你将指针移到文件末尾了filelen = ftell(TestFile);
cout < <filelen;
char *linebuf=new char[filelen];
//初始化扫描类
wordanalyse wordAnalyse(TestFile,linebuf,filelen ); // 在你的wordanalyse里找不到将TestFile指针移到文件头的语句

所以你这样试一下, 在调用构造函数前, 将文件指针移回去:
//初始化扫描类
rewind(TestFile);
wordanalyse wordAnalyse(TestFile,linebuf,filelen );
la_feng 2009-03-06
  • 打赏
  • 举报
回复
#ifndef WORDANALYSE_H
#define WORDANALYSE_H

#include<stdio.h>
#include<string>
//using std::string;
#include<iostream>
using namespace std;

//定义所有的关键字
typedef enum
{
ENDOFFILE, ERROROFFILE,
//key word
ELSE, IF, INT, RETURN, WHILE, VO,
//multicharacer tokens
ID, NUM,
//special symbols
comma, semicolon, leftcurly, rightcurly, leftparen,
rightparen,lessequal, moreequal, eq, noequal, leftexplain ,
rightexplain, leftbracket, rightbracket, plus, minus,
multiply, divided,evaluation, less, more
}TokenType;

//定义每个字符的类型
typedef enum
{
START, INASSIGN, INCOMMENT, INNUM, INID, DONE,
}StateType;


class wordanalyse
{

public:

wordanalyse( FILE *_file ,
char *_linebuf,
int _filelen );

~wordanalyse();

//开始进行词法扫描
void beginScan();


private:


//词法分析的核心类
void getToken( );

//查找当前的字符是否是否程序默认的关键字
void reservedLookup( string str );

//如果获取下一个字符失败就后退一格
void ungetNextChar();

//获取下一个字符
char getNextChar(void);

//输出相关的符合
void printPunctuation( int lineno, string &temp, string punctuation );

//输出相关的关键字
void printKeyWord( int lineno, string &temp, string word, char passString[] );


//输出相关的信息
void printToken( TokenType token, char tokenString[] );

public:

string m_path; //存储扫描结果

private:

int filelen; //文件的长度

int lineno; //语句的行数

TokenType currentToken; //代替返回currentToken

int linepos; //在bufline中的位置

int bufsize; //buffer目前的大小

bool EOF_flag; //是否已到文件尾

char *linebuf; //全部代码的字符串

char tokenString[41];//存儲标识符

FILE *file; //文件指针

bool EchoSource;

bool TraceScan;

int zeng;

string tempofchar;

};

#endif
la_feng 2009-03-06
  • 打赏
  • 举报
回复
/*******************************************************
作用: 输出关键字
参数: @ lineno: 行号
@ tem:输出结果的字符串
@ word:字符的类型,系统默认
@ passString:当前输出的字符串部分
返回值:无
*******************************************************/
void wordanalyse::printKeyWord( int lineno, string &temp, string word, char passString[])
{
temp+=word;
temp+=passString;
temp+="\r\n";
}

/*******************************************************
作用: 输出关键字
参数: @ lineno: 行号
@ tem: 输出结果的字符串
@ punctuation: 标点,系统默认
返回值:无
*******************************************************/
void wordanalyse::printPunctuation( int lineno, string &temp, string punctuation )
{
temp+=punctuation;
temp+="\r\n";

}

/*******************************************************
作用: 用有限状态机检查每个字符
参数: 无
返回值:无
*******************************************************/
void wordanalyse::getToken( )
{

StateType state=START;
bool save;

int tokenStringIndex=0;

while( state!= DONE )
{
//获取下一个字符
char c=getNextChar();
save=true;
switch( state )
{
case START: //开始状态
//是否数字
if( isdigit(c) )
{
state=INNUM;
break;
}
//是否字母
else if ( isalpha(c) )
{
state=INID;
break;
}
else if( c=='=' )
{
if( linebuf[linepos]=='=')
{
tokenString[tokenStringIndex++]='=';
linepos+=2;
tokenString[tokenStringIndex++]='=';
state=DONE;
save=false;
currentToken=eq;
tokenString[tokenStringIndex] ='\0';

break;
}
else
{
save=true;
state=DONE;
currentToken=evaluation;
break;
}
}

else if( (c==' ') | (c=='\t') | (c=='\n') )
{
save=false;
break;
}
else if( c=='/' )
{
if( linebuf[linepos]=='*')
{
tokenString[tokenStringIndex++]='/';
linepos+=2;
tokenString[tokenStringIndex++]='*';
state=INCOMMENT;
save=false;
break;
}
else
{
state=DONE;
currentToken=divided;
break;
}

}
else
{
state=DONE;
switch (c)
{
case EOF:
save=false;
currentToken= ENDOFFILE;
break;
case ',':
currentToken=comma;
break;
case '+':
currentToken=plus;
break;
case ';':
currentToken=semicolon;
break;
case '{':
currentToken=leftcurly;
break;
case '}':
currentToken=rightcurly;
break;
case '(':
currentToken=leftparen;
break;
case ')':
currentToken=rightparen;
break;
case '[':
currentToken=leftbracket;
break;
case ']':
currentToken=rightbracket;
break;
case '!': //判断是'!'还是错误
if( linebuf[linepos]=='=')
{
tokenString[tokenStringIndex++]='!';
linepos+=2;
tokenString[tokenStringIndex++]='=';
state=DONE;
save=false;
currentToken=noequal;
tokenString[tokenStringIndex] ='\0';
break;
}
else
{
save=false;
currentToken=ERROROFFILE;
break;
}

case '*':

currentToken=multiply;
break;
case '-':
currentToken=minus;
break;
case '/':

case '<': //判断是'<='还是'<'
if( linebuf[linepos]=='=')
{
tokenString[tokenStringIndex++]='<';
linepos+=2;
tokenString[tokenStringIndex++]='=';
state=DONE;
save=false;
tokenString[tokenStringIndex] ='\0';
currentToken=lessequal;
break;
}
else
{
currentToken=less;
break;
}
case '>': //判断是'>='还是'>'
if( linebuf[linepos]=='=')
{
tokenString[tokenStringIndex++]='>';
linepos+=2;
tokenString[tokenStringIndex++]='=';
state=DONE;
save=false;
tokenString[tokenStringIndex] ='\0';
currentToken=lessequal;
break;
}
else
{
currentToken=more;
break;
}
default :
currentToken=ERROROFFILE;
break;
}//end of switch_Punctuation
}//end of else
case INCOMMENT: //是注释
save=false;
if( c==EOF )
{
state=DONE;
currentToken=ENDOFFILE;
}
else
{
if( c=='*' && linebuf[linepos]=='/' )
{
state=START;
linepos+=1;

}
}
break;
case INNUM: //是数字
if (!isdigit(c))
{ // backup in the input
ungetNextChar();
save = false;
state = DONE;
currentToken = NUM;
}
break;
case INID: //是字母
if (!isalpha(c))
{
ungetNextChar();
save = false;
state = DONE;
currentToken = ID;
}
break;
case DONE:
break;
default:
m_path+="Scanner Error\r\n";
state = DONE;
currentToken = ERROROFFILE;
break;
}//end of switch_state
if ((save) && (tokenStringIndex <= 41))
tokenString[tokenStringIndex++] = c;
if (state == DONE)
{
tokenString[tokenStringIndex] ='\0';
if (currentToken == ID)
{
//查找是否程序默认的关键字
reservedLookup(tokenString);
}
}


}//end of while

if( TraceScan )
{
m_path+="\t";
// tempofchar.Format( "%d: ", lineno );
m_path+=tempofchar;
printToken( currentToken, tokenString );
}

}

/*******************************************************
作用: 获取下一个字符
参数: 无
返回值:下一个字符
*******************************************************/
char wordanalyse::getNextChar()
{
if( !( linepos < bufsize ) )
{
lineno++;
if( fgets( linebuf,filelen, file ) )
{
if(EchoSource)
{
string x="tempOfX";
// x.Format( "%d", lineno );
m_path+=x;
m_path+=": ";
m_path+=linebuf;
m_path+="\r\n";
}
bufsize = strlen( linebuf );
linepos = 0;
return linebuf[linepos++];
}
else //到了尾
{
EOF_flag = true;
return EOF;
}

}
else
return linebuf[linepos++];

}

/*******************************************************
作用: 获取下一个字符无效后重新设置
参数: 无
返回值:无
*******************************************************/
void wordanalyse::ungetNextChar()
{
if( !EOF_flag )
{
linepos=linepos-1;
}
}

/*******************************************************
作用: 查找当前的字符是否是否程序默认的关键字
参数: @ 要查找的字符串
返回值:无
*******************************************************/
void wordanalyse::reservedLookup( string str )
{

if(str=="else")
currentToken=ELSE;
else if(str=="if")
currentToken=IF;
else if(str=="int")
currentToken=INT;
else if(str=="return")
currentToken=RETURN;
else if( str=="void")
currentToken=VO;
else if(str=="while")
currentToken=WHILE;
else
currentToken=ID;
}
la_feng 2009-03-06
  • 打赏
  • 举报
回复
#include "wordanalyse.h"



/*******************************************************
作用: 开始进行词法扫描
参数: @ _file: 文件指针
@ _linebuf: 字符缓冲区
@ _file: 文件长度
返回值:无
*******************************************************/
wordanalyse::wordanalyse( FILE *_file ,
char *_linebuf,
int _filelen )
{
linepos=0; //在bufline中的位置

bufsize=0; //目前buffer中字符串的大小

EOF_flag=false; //是否已到文件尾

EchoSource=true;

TraceScan=true;

lineno=0;//幕前的行数

file=_file; //获取文件指针

linebuf=_linebuf; //字符缓冲区

filelen=_filelen; //文件的长度


}

wordanalyse::~wordanalyse()
{

}

/*******************************************************
作用: 开始进行词法扫描
参数: 无
返回值:无
*******************************************************/
void wordanalyse::beginScan()
{
while( currentToken!=ENDOFFILE )
{
getToken();
}

}

/*******************************************************
作用: 输出相关的信息
参数: @ token:字符的类型
@ tokenString[]:当前输出的字符串部分
返回值:无
*******************************************************/
void wordanalyse::printToken(TokenType token, char tokenString[] )
{
//根据token输出相关的内容
switch( token )
{
case ELSE:
case IF:
case INT :
case RETURN:
case WHILE:
printKeyWord( lineno, m_path, "reserved word: ", tokenString );
break;
case VO:
printKeyWord( lineno, m_path, "reserved word: ", "void");
break;

case comma:
printPunctuation( lineno, m_path, "," );
break;
case leftcurly:
printPunctuation( lineno, m_path, "{" );
break;
case rightcurly:
printPunctuation( lineno, m_path, "}" );
break;
case leftparen:
printPunctuation( lineno, m_path, "(" );
break;
case semicolon:
printPunctuation( lineno, m_path, ";" );
break;

case rightparen:
printPunctuation( lineno, m_path, ")" );
break;
case lessequal:
printPunctuation( lineno, m_path, "<=" );
break;
case eq:
printPunctuation( lineno, m_path, "==" );
break;
case noequal:
printPunctuation( lineno, m_path, "!=" );
break;
case leftbracket:
printPunctuation( lineno, m_path, "[" );
break;
case plus:
printPunctuation( lineno, m_path, "+" );
break;
case minus:
printPunctuation( lineno, m_path, "-" );
break;
case multiply:
printPunctuation( lineno, m_path, "*" );
break;
case divided:
printPunctuation( lineno, m_path, "/" );
break;
case evaluation:
printPunctuation( lineno, m_path, "=" );
break;
case less:
printPunctuation( lineno, m_path, "<" );
break;
case more:
printPunctuation( lineno, m_path, ">" );
break;
case NUM:
printKeyWord( lineno, m_path, "NUM, val= ", tokenString );
break;
case ID:
printKeyWord( lineno, m_path, "ID, name= ", tokenString );
break;
case ERROROFFILE:
printKeyWord( lineno, m_path, "ERROR ", tokenString );
break;
case ENDOFFILE:
printKeyWord( lineno, m_path, "EOF ", tokenString );
break;

default:
printKeyWord( lineno, m_path, "Unknown token= ", tokenString );
break;

}
}
lightnut 2009-03-06
  • 打赏
  • 举报
回复
不知道你的wordanalyse 是怎么写的, 难道没有
rewind(TestFile)
  • 打赏
  • 举报
回复
up先

65,187

社区成员

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

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