[英]C code works on linux(geany) but not on windows(VS2008)
我已經為一個程序編寫了代碼,在該程序中,用戶輸入.txt
文件的名稱,並且該程序打印文件中找到的字母數字單詞或數字(字母數字單詞以字母開頭,包括字母或數字)。 無論如何,當在使用Geany的Linux上運行時,它可以正常工作,但在Visual Studio 2008上卻不能(我使用此視頻允許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' )
;
}
Visual Studio版本-您已將指針分配給本地數組sem-> s = lexeme; 在yylex()內部。 因此,當調用返回到main時,lexeme中的值將變為無效。 因此,您必須分配內存,然后將值從lexeme復制到sem->。 按照代碼中的// change注釋查看更改:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include<malloc.h>//change
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=(char *)malloc(sizeof(char)*(i+2));//change
memcpy(sem->s,lexeme, i+2);//change
//sem->s = lexeme;//change
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' );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.