user-0 提问: Visual C++编译时 expression:stream!=NULL

回复:
user: user-0
content: 引用 6 楼 棉猴的回复:if((fp=fopen(fname,"r"))==NULL)//=改成==试试 OK，问题解决了，多谢

user: user-1
content: if((fp=fopen(fname,"r"))==NULL)//=改成==试试

user: user-1
content: if((fp=fopen(fname,"r"))=NULL)   {      printf("Error!--Can't open file.");      getchar();      exit(1);   }   else{       scanner_example(fp);//-----------fp是NULL-------   } 导致 void scanner_example(FILE *fp) {     char ch;      int i,c;     ch=fgetc(fp);//------fp是NULL，所以报错！----------    ....... }

user: user-2
content: 应该是文件流那里有问题。 if((fp=fopen(fname,"r"))=NULL)  r改成rb试试

user: user-0
content: 引用 1 楼 棉猴的回复:把完整代码贴出来吧。另外VC++6.0是我的最爱，没有之一 2楼是我发的完整的源代码

user: user-0
content: #include<stdlib.h> #include<stdio.h> #include<ctype.h> #include<string.h> #include<math.h> #define MAX_KEY_NUMBER 20 #define KEY_WORD_END "waiting for your expanding" #define ID 6 #define INT 7 #define LT 8 #define LE 9 #define EQ 10 #define NE 11 #define GT 12 #define GE 13 #define IS 14 #define PL 15 #define MI 16 #define MU 17 #define DI 18 #define UCON 19 char TOKEN[20]; int lookup(char*);  void out(int, char*);  void report_error(); void scanner_example(FILE *fp) {     char ch;      int i,c;     ch=fgetc(fp); 	int checkdot=0;      int checke=0;     int t;     t=fgetc(fp);  	fseek(fp,-1,1);    while(t!=EOF)   {      if(isalpha(ch))  	  {       TOKEN[0]=ch;  	  ch=fgetc(fp); 	  i=1;     while(isalnum(ch)) 	 {       TOKEN[i]=ch; 	  i++;  	  ch=fgetc(fp); 	  }       TOKEN[i]='\0';       fseek(fp,-1,1);        c=lookup(TOKEN);      if(c==0)  		  out(ID, TOKEN);  	 else out(c," "); 	 }      else if(isdigit(ch)) 	 {       TOKEN[0]=ch;  	  ch=fgetc(fp); 	  i=1;    while (isdigit(ch))    {       TOKEN[i]=ch;  	  i++;  	  ch=fgetc(fp);  	  }       TOKEN[i]='\0';       fseek(fp,-1,1);       out(INT,TOKEN); 	}     else     if(isdigit(ch)) 	{       TOKEN[0]=ch;       ch=fgetc(fp);       i=1;     while(isdigit(ch)||ch=='.'||ch=='E'||ch=='e')  	{ 		if (ch=='.'){checkdot=1;} 		else if(ch=='e'||ch=='E') 		{       checke=1;        TOKEN[i]=ch;       i++;       ch=fgetc(fp);     if(ch=='+') 	{ 		TOKEN[i]=ch; 		i++;         ch=fgetc(fp); 	  {       TOKEN[i]=ch;       i++;       ch=fgetc(fp);        continue; 	  } 	 }     else     { 	  TOKEN[i]=ch;       i++;       ch=fgetc(fp); 	  {       TOKEN[i]=ch;       i++;       ch=fgetc(fp);       continue; 	  } 	 }     if (ch=='-') 	{ 	  TOKEN[i]=ch; 	  i++;       ch=fgetc(fp); 	 {       TOKEN[i]=ch;       i++;       ch=fgetc(fp);       continue;       } 	}       TOKEN[i]=ch; 	  i++;       ch=fgetc(fp); 	  }       TOKEN[i]='\0';       fseek(fp,-1,1);    if(checkdot==1) 	{       out(UCON,TOKEN); 	  }    else if (checke==1) 	{       out(UCON,TOKEN); 	  }     else       out(INT,TOKEN); 	  } 	 }     else     switch (ch)  	{       case '<': ch= fgetc(fp);      if (ch=='=') out(LE, " ");       else if (ch=='>') out(NE, " ");      else 	 {       fseek(fp,-1,1); 	  TOKEN[i]=ch;       i++;       ch=fgetc(fp);       out(LT," ");  	  }       break;       case '=': out(EQ," "); break;       case ':': ch=fgetc(fp);      if(ch=='=') out(IS," ");      else  	 {       fseek(fp,-1,1);       report_error(); 	  }       break;       case '>': ch=fgetc(fp);      if (ch=='=') out(GE, " ");      else 	 {       fseek(fp,-1,1);       out(GT," "); 	  }       break;       case '+':out(PL," ");break;       case '-':out(MI," ");break;       case '*':out(MU," ");break;       case '/':out(DI," ");break;       case '\n': break;       case ' ': break;       case '\t': break;       default: report_error();break; 	  }       ch=fgetc(fp);       t=fgetc(fp);       fseek (fp,-1,1); 	  }     return;  }   char *KeyWordTable[MAX_KEY_NUMBER]={"begin","end","if","then","else",KEY_WORD_END};  int lookup(char *token)  {     int n= 0;     while(strcmp(KeyWordTable[n],KEY_WORD_END))  	{      if (!strcmp(KeyWordTable[n],token)) 	  {       return n+1;        break; 	  }       n++; 	  }       return 0;  	 }     void report_error() 	{      printf("error");  	 }     void out(int c, char* v) 	{       char *cl="";      switch (c) 	 {       case  1: cl="BEGIN"; break;       case  2: cl="END"; break;       case  3: cl="IF"; break;       case  4: cl="THEN"; break;       case  5: cl="ELSE"; break;       case  6: cl="ID"; break;       case  7: cl="INT"; break;       case  8: cl="LT"; break;        case  9: cl="LE"; break;       case  10: cl="EQ"; break;       case  11: cl="NE"; break;       case  12: cl="GT"; break;       case  13: cl="GE"; break;       case  14: cl="IS"; break;        case  15: cl="PL"; break;       case  16: cl="MI"; break;       case  17: cl="MU"; break;       case  18: cl="DI"; break;       case  19: cl="UCON"; break; 	  }       printf("(%s,%s)\n",cl,v); 	  } int main(void) {   char *fname="file.txt";   FILE *fp;   if((fp=fopen(fname,"r"))=NULL)   {      printf("Error!--Can't open file.");      getchar();      exit(1); 	  }   else{       scanner_example(fp); 	  }    fclose(fp);    return 1;  }

user: user-1
content: 把完整代码贴出来吧。另外VC++6.0是我的最爱，没有之一