txt文件最后一位是什么?

lilimerci 2007-04-05 04:43:02
(词法分析)源程序:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <alloc.h>
#include <conio.h>

int IsLetter(char ch)
{/*判断是否是字母,是则返回 1,否则返回 0 */
if(isalpha(ch)) return 1;
return 0;
}
int IsDigit(char ch)
{/*判断是否为数字或字母,是则返回 1,否则返回 0 */
if(isalnum(ch)) return 1;
return 0;
}
int IsSpace(char ch)
{/*判断是否为空白符(空格、换行、制表符等),是则返回 1,否则返回 0*/
if(isspace(ch)) return 1;
return 0;
}
void GetChar(FILE *fp,char *ch)
{/*读取字符送ch*/
*ch = fgetc(fp);
}
void GetBC(FILE *fp,char *ch)
{/*如果是空白则继续读下一个,直到不是空白*/
do {
GetChar(fp,ch);
}while(IsSpace(*ch)&&(*ch != EOF));
}
void Retract(FILE *fp,char *ch)
{/*光标回退一位,并使 ch 为空*/
fseek(fp,-1,1);
*ch = '';
}
char Reserve(char *strToken)
{/*返回关键字的编码*/
if(strcmp(strToken,"main") == 0) return '2';
if(strcmp(strToken,"int") == 0) return '1';
if(strcmp(strToken,"char") == 0) return '3';
if(strcmp(strToken,"if") == 0) return '4';
if(strcmp(strToken,"else") == 0) return '5';
if(strcmp(strToken,"for") == 0) return '6';
if(strcmp(strToken,"while") == 0) return '7';
return '0';
}
void Concat(char *strToken, char *ch)
{/*将ch中的字符连接到strToken后面*/
int i;
for(i=0;i<80;i++) {
if(*strToken == NULL) {
*strToken = *ch;
break;
}
strToken++;
}
}

int lexSubFunc(FILE *fp1,FILE *fp2)
{/*词法分析子程序*/
char ch,code;
int i;
char strToken[80];
while(1) {
GetBC(fp1,&ch);
for(i=0;i<80;i++) strToken[i]=NULL;
if(ch == EOF) return 0;
if (IsLetter(ch)) {
while (IsLetter(ch) || IsDigit(ch)) {
Concat(strToken,&ch);
GetChar(fp1,&ch);
}
Retract(fp1,&ch);
code = Reserve(strToken);
if (code == '0') {
printf("(10,%-6s)\n",strToken);
fputs("(10,\"",fp2); fputs(strToken,fp2); fputs("\")\n",fp2);
}
else {
printf("(%2c,%-6s)\n",code,strToken);
fputs("(",fp2); fputc(code,fp2);fputs(",\"",fp2);fputs(strToken,fp2); fputs("\")\n",fp2);
}
}
else if (IsDigit(ch)) {
while (IsDigit(ch)) {
Concat(strToken,&ch);
GetChar(fp1,&ch);
}
Retract(fp1,&ch);
printf("(20,%-6s>\n",strToken);
fputs("(20,\"",fp2); fputs(strToken,fp2); fputs("\")\n",fp2);
}
else if (ch == '=') {
GetChar(fp1,&ch);
if(ch == '='){
printf("(39,==%3c)\n",' ');
fputs("(39,\"==\")\n",fp2);
}
else{
Retract(fp1,&ch);
printf("(21,=%5c)\n",' ');
fputs("(21,\"=\")\n",fp2);
}
}
else if (ch == '+') {
printf("(22,+%5c)\n",' ');
fputs("(22,\"+\")\n",fp2);
}
else if (ch == '-') {
printf("(23,-%5c)\n",' ');
fputs("(23,\"-\")\n",fp2);
}
else if (ch == '*') {
printf("(24,*%5c)\n",' ');
fputs("(24,\"*\")\n",fp2);
}
else if (ch == '/') {
printf("(25,/%5c)\n",' ');
fputs("(25,\"/\")\n",fp2);
}
else if (ch == '(') {
printf("(26,(%5c)\n",' ');
fputs("(26,\"(\")\n",fp2);
}
else if (ch == ')') {
printf("(27,)%5c)\n",' ');
fputs("(27,\")\")\n",fp2);
}
else if (ch == '[') {
printf("(28,[%5c)\n",' ');
fputs("(28,\"[\")\n",fp2);
}
else if (ch == ']}') {
printf("(29,]%5c)\n",' ');
fputs("(29,\"]\")\n",fp2);
}
else if (ch == '{') {
printf("(30,{%5c)\n",' ');
fputs("(30,\"{\")\n",fp2);
}
else if (ch == '}') {
printf("(31,}%5c)\n",' ');
fputs("(31,\"}\")\n",fp2);
}
else if (ch == ',') {
printf("(32,,%5c)\n",' ');
fputs("(32,\",\")\n",fp2);
}
else if (ch == ':') {
printf("(33,:%5c)\n",' ');
fputs("(33,\":\")\n",fp2);
}
else if (ch == ';') {
printf("(34,;%5c)\n",' ');
fputs("(34,\";\")\n",fp2);
}
else if (ch == '>') {
GetChar(fp1,&ch);
if(ch == '='){
printf("(37,>=%4c)\n",' ');
fputs("(37,\">=\")\n",fp2);
}
else{
Retract(fp1,&ch);
printf("(35,>%5c)\n",' ');
fputs("(35,\">\")\n",fp2);
}
}
else if (ch == '<') {
GetChar(fp1,&ch);
if(ch == '='){
printf("(38,<=%4c)\n",' ');
fputs("(38,\"<=\")\n",fp2);
}
else{
Retract(fp1,&ch);
printf("(36,<%5c)\n",' ');
fputs("(36,\"<\")\n",fp2);
}
}
else if (ch == '!') {
GetChar(fp1,&ch);
if(ch == '='){
printf("(40,!=%3c)\n",' ');
fputs("(40,\"!=\")\n",fp2);
}
else
Retract(fp1,&ch);
}
else if (ch == '\0') {
printf("(1000,\0%3c)\n",' ');
fputs("(1000,\"\0\")\n",fp2);
}
}
}
void main()
{/*主程序*/
FILE *fp1,*fp2;
if((fp1=fopen("input.txt","rt"))==NULL)
{
printf("Cannot open input.txt\n");
getch(); exit(1);
}
if((fp2=fopen("out.txt","wt+"))==NULL)
{
printf("Cannot create out.txt FILE.strike any key exit");
getch(); exit(1);
}
lexSubFunc(fp1,fp2);
fclose(fp1);
fclose(fp2);
getch();
}


如果input.txt文件内容为:{9*a
运行后进入死循环(注意a后面没有空格)
如果input.txt文件内容为:{9*a}
运行后程序可以正常结束。

同样是当ch==EOF是退出的,为什么第一种就退不了呢?

各位GGJJ帮我看看啊
谢谢了
...全文
630 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
lilimerci 2007-04-11
  • 打赏
  • 举报
回复
ciahi(爪哇乞丐) 谢谢。
ciahi 2007-04-11
  • 打赏
  • 举报
回复
#include "Stdio.h"
#include "Conio.h"
#include <ctype.h>

int main(void)
{
FILE *fp;
char ch;
if((fp=fopen("out.txt","rb"))==NULL)
{
printf("Cannot open input.txt\n");
getch();
exit(1);
}
fseek(fp,0L,2);/*到文件末尾*/
ch = fgetc(fp);/*读文件,返回EOF*/
ch = fgetc(fp);/*再读还是返回EOF*/
fseek(fp,2L,0);/*重新定位*/
ch = fgetc(fp); /*注意,加上这一句,这样才能正确判断 本句*/
if(ch != EOF)
printf("%c",ch);/*如果可以重定位那么就输出该字符*/
else
printf("Error.");/*如果不可以重定位那么输出Error*/
fclose(fp);
getch();
return 0;
}


ch = fgetc(fp); /*注意,加上这一句,这样才能正确判断 本句*/
lilimerci 2007-04-08
  • 打赏
  • 举报
回复
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;

public class Cifa {

/**
* @param args
*/
static int judge = 1;
/**用于保存分析结果*/
static ArrayList<String> list = new ArrayList<String>();
public static void main(String[] args) throws IOException{
RandomAccessFile infile = null;
RandomAccessFile outfile = null;
infile = new RandomAccessFile("input.txt","rw");
outfile = new RandomAccessFile("output.txt","rw");
Assay(infile);
System.out.println("The contents of the input.txt is:");
infile.seek(0);
for(int i = 0;i < infile.length();i++){
System.out.print((char)infile.readByte());
}
System.out.println("\nAfter assayint,the contents of the output.txt is:");
for(int i = 0;i < list.size();i++){

System.out.print(list.get(i));
}
for(int i = 0;i <list.size();i++){
outfile.writeBytes(list.get(i));
}
if(infile!=null){
infile.close();
}
if(outfile!=null){
outfile.close();
}
}

/**判断是否为字母*/
static boolean IsLetter(char ch){
if(('a'<= ch && ch <= 'z')||('A'<= ch && ch <= 'Z')) return true;
return false;
}
/**判断是否为数字*/
static boolean IsNumber(char ch){
if('0'<= ch && ch <= '9') return true;
return false;
}
/**判断是否为空字符*/
static boolean IsSpace(char ch){
if(ch == ' ' || ch == '\t' || ch == '\r'
|| ch == '\f' || ch == '\n') return true;
return false;
}

/**获得一个非空字符*/
static char GetBc(RandomAccessFile file){
char temp = ' ';
byte[] buf = new byte[1];
try {
do{
judge = file.read(buf);
temp = (char)buf[0];
}while(IsSpace(temp) && judge != -1);
} catch (IOException e) {
e.printStackTrace();
}
return temp;
}

/**词法分析函数
* @throws IOException */
static int Assay(RandomAccessFile file) throws IOException{
char ch = ' ';
boolean status;
long index1,index2;
String str;
while(judge!=-1){
ch = GetBc(file);
index1 = file.getFilePointer();
status = true;
if(IsLetter(ch)&&status){/*字母开头*/
str = "";
index2 = 0;
while((IsLetter(ch)||IsNumber(ch))&&status){
str = str.concat(String.valueOf(ch));
ch = GetBc(file);
index2++;
if((file.getFilePointer() - index1) != index2){
status = false;
}
}
file.seek((file.getFilePointer()-1));
ch = ' ';
InsertItem(list,str);
}
else if(IsNumber(ch)&&status){/*数字开头*/
str = "";
index2 = 0;
while(IsNumber(ch)&&status){
str = str.concat(String.valueOf(ch));
ch = GetBc(file);
index2++;
if((file.getFilePointer() - index1) != index2){
status = false;
}
}
file.seek((file.getFilePointer()-1));
ch = ' ';
InsertItem(list,str);
}
else if(ch == '='){
if(judge!=-1){
ch = GetBc(file);
if(ch == '=')
InsertItem(list,"==");
else{
file.seek((file.getFilePointer()-1));
InsertItem(list,"=");
}
}
else
System.out.println("("+21+",\"=\")");
}
else if(ch == '+')
InsertItem(list,"+");
else if(ch == '-')
InsertItem(list,"-");
else if(ch == '*')
InsertItem(list,"*");
else if(ch == '/')
InsertItem(list,"/");
else if(ch == '(')
InsertItem(list,"(");
else if(ch == ')')
InsertItem(list,")");
else if(ch == '[')
InsertItem(list,"[");
else if(ch == ']')
InsertItem(list,"]");
else if(ch == '{')
InsertItem(list,"{");
else if(ch == '}')
InsertItem(list,"}");
else if(ch == ',')
InsertItem(list,",");
else if(ch == ':')
InsertItem(list,":");
else if(ch == ';')
InsertItem(list,";");
//else if(ch == '\0')
// InsertItem(list,"\0");
else if(ch == '>'){
if(judge!=-1){
ch = GetBc(file);
if(ch == '=')
InsertItem(list,">=");
else{
file.seek((file.getFilePointer()-1));
InsertItem(list,">");
}
}
else
InsertItem(list,">");
}
else if(ch == '<'){
if(judge!=-1){
ch = GetBc(file);
if(ch == '=')
InsertItem(list,"");
else{
file.seek((file.getFilePointer()-1));
InsertItem(list,"<=");
}
}
else
InsertItem(list,"<");
}
else if(ch == '!'){
if(judge!=-1){
ch = GetBc(file);
if(ch == '=')
InsertItem(list,"!=");
else{
//file.seek((file.getFilePointer()-1));
//System.out.println("("+35+",\">\")");
}
}
//else
//System.out.println("("+35+",\">\")");
}
}
return 0;
}
/**关键字和特殊符号编码*/
static String Reserve(String str){
if(str.equals("main")) return "2";
if(str.equals("int")) return "1";
if(str.equals("char")) return "3";
if(str.equals("if")) return "4";
if(str.equals("else")) return "5";
if(str.equals("for")) return "6";
if(str.equals("while")) return "7";
if(str.equals("=")) return "21";
if(str.equals("+")) return "22";
if(str.equals("-")) return "23";
if(str.equals("*")) return "24";
if(str.equals("/")) return "25";
if(str.equals("(")) return "26";
if(str.equals(")")) return "27";
if(str.equals("[")) return "28";
if(str.equals("]")) return "29";
if(str.equals("{")) return "30";
if(str.equals("}")) return "31";
if(str.equals(",")) return "32";
if(str.equals(":")) return "33";
if(str.equals(";")) return "34";
if(str.equals(">")) return "35";
if(str.equals("<")) return "36";
if(str.equals(">=")) return "37";
if(str.equals("<=")) return "38";
if(str.equals("==")) return "39";
if(str.equals("!=")) return "40";
//if(str.equals("\0")) return "1000";
return "10";
}
/**格式化字串并插入到容器中*/
static void InsertItem(ArrayList<String> list,String str){
int strLength = 8;
String newString = "\"";
newString += str + "\"";
int cutLength = strLength-newString.length();
for(int i=0;i<cutLength;i++)
newString +=" ";
if(Reserve(str).length() < 2){
newString = "( " + Reserve(str) + "," + newString + ")\r\n";
list.add(newString);
}
else{
newString = "(" + Reserve(str) + "," + newString + ")\r\n";
list.add(newString);
}
}
}
lilimerci 2007-04-07
  • 打赏
  • 举报
回复
各位辛苦了,我自己搞定这个问题了。
我做了个测试:
如果读过了文件的最后一个字符,那么再往下读文件指针会指向什么地方?可否重定义指针位置?
答案是这样的:具体指针指向了什么地方我不知道,总之不可以重新定义指针的位置。
测试程序如下:

#include "Stdio.h"
#include "Conio.h"
#include <ctype.h>

int main(void)
{
FILE *fp;
char ch;
if((fp=fopen("out.txt","rb"))==NULL)
{
printf("Cannot open input.txt\n");
getch();
exit(1);
}
fseek(fp,0L,2);/*到文件末尾*/
ch = fgetc(fp);/*读文件,返回EOF*/
ch = fgetc(fp);/*再读还是返回EOF*/
fseek(fp,2L,0);/*重新定位*/
if(ch != EOF)
printf("%c",ch);/*如果可以重定位那么就输出该字符*/
else
printf("Error.");/*如果不可以重定位那么输出Error*/
fclose(fp);
getch();
return 0;
}
lilimerci 2007-04-07
  • 打赏
  • 举报
回复
雁南飞你好:
你说的对,EOF的确不是txt文件的最后一位。
如果读过最后一个字符,再读下一个字符时会返回错误,那么返回错误后文件指针还变么?
假设有如下文件(test.txt文件,叹号后面没有内容):
test.txt 里面存有:This is a test!

我读到了叹号'!'后,如果再读必然会返回EOF。那么这个时候文件指针会指向什么地方呢?
还有我用fseek(fp,-1,1); 回退一个字节是否又指向文件末尾了呢(叹号的后面)?
我回退2个字节是否就指向文件的最后一个字节'!'叹号字符呢?

期待你的答复,谢谢。
飞哥 2007-04-07
  • 打赏
  • 举报
回复
txt有最后一位吗?
应该没有

判断文件结束用 EOF 但是它不是文件的字符,只是一个错误标识,是读过最后一个字符,再读下一个字符时返回的错误
lilimerci 2007-04-06
  • 打赏
  • 举报
回复
还是不行
liuyaoyou 2007-04-06
  • 打赏
  • 举报
回复
fopen("input.txt","rt")改成fopen("input.txt","rb")试试,你那是以文本方式打开的。
lilimerci 2007-04-05
  • 打赏
  • 举报
回复
回车我知道
我是想知道出现那问题的原因。
谢谢。
fly4free 2007-04-05
  • 打赏
  • 举报
回复
你楼上说的是回车(文本模式的'\n')。
lilimerci 2007-04-05
  • 打赏
  • 举报
回复
楼上的能说清楚点么
谢谢
leirende 2007-04-05
  • 打赏
  • 举报
回复
0d 0a
lilimerci 2007-04-05
  • 打赏
  • 举报
回复
大家好好看问题啊!
我是想问 为什么以字符或者字母结束文件 会死循环?
sms88 2007-04-05
  • 打赏
  • 举报
回复
叫结束符
  • 打赏
  • 举报
回复
字符
ciahi 2007-04-05
  • 打赏
  • 举报
回复
char类型的一个标志
char buf;
buf = fgetc(fp);
if(buf == EOF)
{
文件结束
}
iu_81 2007-04-05
  • 打赏
  • 举报
回复
EOF

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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