【发布时间】:2014-02-16 21:54:45
【问题描述】:
我为一个程序编写了代码,其中用户输入.txt 文件的名称,程序打印文件中找到的字母数字单词或数字(字母数字单词以字母开头,包括字母或数字)。无论如何,当使用 Geany 在 Linux 上运行时,它可以正常工作,但在 Visual Studio 2008 上却不行(我使用this video 允许 VS2008 编译 c 代码)。
确切的问题是它显示的字母数字就像表情符号。还有一个无限循环,我认为这可能与我使用 fseek 函数有关。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
union semantic_info
{
char *s;
int i;
}SEMANTIC_INFO ;
int yylex(FILE *fp, union semantic_info *sem);
void WaitForEnter(void);
int main()
{
union semantic_info *ptrs;
char filename[20];
int a=0,n=0;
int k;
FILE *fp;
ptrs = &SEMANTIC_INFO;
printf("Hey you, give me a file name: \n");
scanf("%s", filename);
fp = fopen(filename, "r");
do
{
k = yylex(fp, ptrs);
if( k==1 )
{
printf("%s%s\n", "The type is alphanumeric and it's the: ", SEMANTIC_INFO.s);
a++;
}
else if( k==2 )
{
printf("%s%d\n", "The type is arithmetic and it's the: ", SEMANTIC_INFO.i);
n++;
}
}while( k!=0 );
printf("we found %d alphanumerics and %d numbers! \n", a, n);
return 0;
}
int yylex(FILE *fp, union semantic_info *sem)
{
int nextcharacter;
char lexeme[100]="";
int k=1, i=0, r=3;
nextcharacter = fgetc(fp);
if ( nextcharacter == EOF)
{
r=0;
}
else if ( isalpha(nextcharacter) != 0 )
{
lexeme[i] = (char)nextcharacter;
//printf("at position %d there is %c \n", i, lexeme[i]);
while(k==1)
{
nextcharacter = fgetc(fp);
if(isalnum(nextcharacter) != 0 )
{
i++;
lexeme[i] = (char)nextcharacter;
//printf("at position %d there is %c \n", i, lexeme[i]);
}
else
{
lexeme[i+1] = '\0';
k=0;
sem->s = lexeme;
r=1;
fseek(fp, -1, SEEK_CUR);
}
}
}
else if ( isdigit(nextcharacter) != 0 )
{
lexeme[i] = (char)nextcharacter;
//printf("at position %d there is %c \n", i, lexeme[i]);
while(k==1)
{
nextcharacter = fgetc(fp);
if(isdigit(nextcharacter)!= 0 )
{
i++;
lexeme[i] = (char)nextcharacter;
//printf("at position %d there is %c \n", i, lexeme[i]);
}
else
{
lexeme[i+1] = '\0';
k=0;
sscanf(lexeme, "%d", &sem->i);
r=2;
fseek(fp, -1, SEEK_CUR);
}
}
}
WaitForEnter();
return r;
}
void WaitForEnter(void)
{
printf("Press Enter to continue: ");
//fflush(stdout);
while ( getchar() != '\n' )
;
}
【问题讨论】:
-
您很可能需要在
while循环中检查nextcharacter中的EOF,因为您描述的问题可能来自Win32 标准库中处理错误输入的差异。
标签: c linux windows visual-studio-2008