文件存在时,fopen返回null

时间:2014-05-20 19:56:42

标签: windows cygwin fopen

我正在使用Windows 8操作系统上的Cygwin终端和flex工具为F ++语言开发词法分析器。

我遇到的问题和我很难解决的问题是多输入缓冲区。

假设我有一个带有#include test.h标头的F ++文件。然后我的词法分析者必须打开 test.h文件并在进入文件之前识别其标记等。

我的词法分析器使用fopen函数打开头文件,但即使我的文件存在,fopen也会返回null。

我引用我的代码。

**<incl>[ \t]*      /* eat the whitespace */
 <incl>[^ \t\n]+   { /* got the include file name */
    if ( include_stack_ptr >= MAX_INCLUDE_DEPTH )
    {
        fprintf( stderr, "Includes nested too deeply" );
        exit( 1 );
    }

    include_stack[include_stack_ptr++] =YY_CURRENT_BUFFER;

    if( (yyin = fopen( yytext, "r,ccs=ccs=UTF-8")) !=NULL)
    {
        printf("Successful opening %s in mode r.\n",yytext);
    }else{
        fprintf(stderr, "\nError opening file %s in mode r.\n",yytext);
    }

    yy_switch_to_buffer(yy_create_buffer( yyin, YY_BUF_SIZE ) );
    printf("testpoint4");
    BEGIN(INITIAL);
    printf("testpoint5");
    }
    <<EOF>> {
    if ( (include_stack_ptr--)==0 ){
    printf("here");
    yyterminate();
    }else{
    printf("edw2");
    yy_delete_buffer( YY_CURRENT_BUFFER );
    yy_switch_to_buffer(include_stack[include_stack_ptr] );
        }
    }

%%
main()
{ yylex(); }**

1 个答案:

答案 0 :(得分:0)

检查一行:fopen(yytext,&#34; r,ccs = ccs = UTF-8&#34;)

使用其中没有逗号的文件名。

为了充分谨慎,请尝试一个简单的文件名,如foo.fpp

如果文件打开,则问题在于文件系统/操作系统上文件的允许字符。