Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C代码可以在linux(geany)上运行,但不能在windows(VS2008)上运行_C_Linux_Windows_Visual Studio 2008 - Fatal编程技术网

C代码可以在linux(geany)上运行,但不能在windows(VS2008)上运行

C代码可以在linux(geany)上运行,但不能在windows(VS2008)上运行,c,linux,windows,visual-studio-2008,C,Linux,Windows,Visual Studio 2008,我为一个程序编写了代码,其中用户输入.txt文件的名称,程序打印文件中的字母数字单词或数字(字母数字单词以字母开头,包括字母或数字)。无论如何,当使用Geany在Linux上运行时,它工作得很好,但在VisualStudio2008上却不行(我曾经允许VS2008编译c代码) 确切的问题是,它显示的字母数字类似于表情符号。还有一个无休止的循环,我相信这可能与我使用的fseek函数有关 #include <stdio.h> #include <ctype.h> #inclu

我为一个程序编写了代码,其中用户输入
.txt
文件的名称,程序打印文件中的字母数字单词或数字(字母数字单词以字母开头,包括字母或数字)。无论如何,当使用Geany在Linux上运行时,它工作得很好,但在VisualStudio2008上却不行(我曾经允许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' )
;
} 
#包括
#包括
#包括
联合语义信息
{
char*s;
int i;
}语义信息;
int-yylex(文件*fp,联合语义信息*sem);
void WaitForEnter(void);
int main()
{
联合语义信息*ptrs;
字符文件名[20];
int a=0,n=0;
int k;
文件*fp;
ptrs=&语义信息;
printf(“嘿,你,给我一个文件名:\n”);
scanf(“%s”,文件名);
fp=fopen(文件名,“r”);
做
{   
k=yylex(fp,ptrs);
如果(k==1)
{
printf(“%s%s\n”,“类型是字母数字的,它是:”,SEMANTIC_INFO.s);
a++;
}
else如果(k==2)
{
printf(“%s%d\n”,“类型是算术的,它是:”,SEMANTIC_INFO.i);
n++;
}
}而(k!=0);
printf(“我们找到了%d个字母数字和%d个数字!\n”,a,n);
返回0;
}
int yylex(文件*fp,联合语义信息*sem)
{
int-nextcharacter;
字符词素[100]=“”;
int k=1,i=0,r=3;
nextcharacter=fgetc(fp);
if(nextcharacter==EOF)
{
r=0;
}
else if(isalpha(nextcharacter)!=0)
{
词素[i]=(char)下一个字符;
//printf(“在位置%d有%c\n”,i,词素[i]);
而(k==1)
{
nextcharacter=fgetc(fp);
if(isalnum(nextcharacter)!=0)
{
i++;
词素[i]=(char)下一个字符;
//printf(“在位置%d有%c\n”,i,词素[i]);
}
其他的
{
词素[i+1]='\0';
k=0;
sem->s=词素;
r=1;
fseek(fp,-1,SEEK_CUR);
}
}
}
else if(isdigit(nextcharacter)!=0)
{
词素[i]=(char)下一个字符;
//printf(“在位置%d有%c\n”,i,词素[i]);
而(k==1)
{
nextcharacter=fgetc(fp);
如果(isdigit(下一个字符)!=0)
{
i++;
词素[i]=(char)下一个字符;
//printf(“在位置%d有%c\n”,i,词素[i]);
}
其他的
{
词素[i+1]='\0';
k=0;
sscanf(词素、%d、&sem->i);
r=2;
fseek(fp,-1,SEEK_CUR);
}
}
}
WaitForEnter();
返回r;
}   
void WaitForEnter(void)
{
printf(“按回车键继续:”;
//fflush(stdout);
而(getchar()!='\n')
;
} 

Visual Studio版本-您已将指针分配给本地数组sem->s=lexeme;在yylex()中。因此,当调用返回main时,lexeme中的值变为void。因此,您必须分配内存,然后将值从lexeme复制到sem->。按照代码中的//更改注释查看更改:

#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' );
} 
#包括
#包括
#包括
#包括//更改
联合语义信息{
char*s;
int i;
}语义信息;
int-yylex(文件*fp,联合语义信息*sem);
void WaitForEnter(void);
int main()
{
联合语义信息*ptrs;
字符文件名[20];
int a=0,n=0;
int k;
文件*fp;
ptrs=&语义信息;
printf(“嘿,你,给我一个文件名:\n”);
scanf(“%s”,文件名);
fp=fopen(文件名,“r”);
做
{   
k=yylex(fp,ptrs);
如果(k==1)
{
printf(“%s%s\n”,“类型是字母数字的,它是:”,SEMANTIC_INFO.s);
a++;
}
else如果(k==2)
{
printf(“%s%d\n”,“类型是算术的,它是:”,SEMANTIC_INFO.i);
n++;
}
}while(k!=0);
printf(“我们找到了%d个字母数字和%d个数字!\n”,a,n);
返回0;
}
int yylex(文件*fp,联合语义信息*sem)
{
int-nextcharacter;
字符词素[100]=“”;
int k=1,i=0,r=3;
nextcharacter=fgetc(fp);
if(nextcharacter==EOF)
{
r=0;
}
else if(isalpha(nextcharacter)!=0)
{
词素[i]=(char)下一个字符;
//printf(“在位置%d有%c\n”,i,词素[i]);
而(k==1)
{
nextcharacter=fgetc(fp);
if(isalnum(nextcharacter)!=0)
{
i++;
词素[i]=(char)下一个字符;
//printf(“在位置%d有%c\n”,i,词素[i]);
}
其他的
{
词素[i+1]='\0';
k=0;
sem->s=(char*)malloc(sizeof(char)*(i+2));//更改
memcpy(sem->s,词素,i+2);//更改
//sem->s=lexeme;//更改
r=1;
fseek(fp,-1,SEEK_CUR);
}
}
}
else if(isdigit(nextcharacter)!=0)
{
词素[i]=(char)下一个字符;
//printf(“在位置%d有%c\n”,i,词素[i]);
而(k==1)
{
nextcharacter=fgetc(fp);
如果(isdigit(下一个字符)!=0)
{
i++;
词素[i]=(char)下一个字符;
//printf(“在位置%d有%c\n”,i,词素[i]);
}
其他的
{
词素[i+1]='\0';
k=0;
sscanf(词素、%d、&sem->i);
r=2;
fseek(fp,-1,SEEK_CUR);
}
}
}
WaitForEnter();
返回r;
}   
void WaitForEnter(void){
printf(“按回车键继续:”;
//fflush(stdout);
而(getchar()!='\n');
} 

while循环时,您很可能需要检查
nextcharacter
中的
EOF