0

我想将三个文件名作为词法分析器 FLEX 的命令行参数,其中三个用于输入文件,其余两个用于输出文件。取一个输入文件名和另一个输出文件名很容易,但我不知道如何在“yyout”中存储多个输出文件名。请帮我解决。

FILE *fin=fopen(argv[1],"r");
FILE *output1 = fopen(argv[2], "w");
if(fin==NULL){
       printf("Cannot open specified file\n");
       return 0;
   }
   

yyin = fin;
yyout =output1;

一个输入文件和一个输出文件都可以,但是如果我想要类似的东西怎么办——

FILE *fin=fopen(argv[1],"r");
FILE *output1 = fopen(argv[2], "w");
FILE *output2= fopen(argv[3], "w");
if(fin==NULL){
        printf("Cannot open specified file\n");
        return 0;
    }
    

yyin = fin;
yyout =output1;

如何将 output1 和 output2 都存储在 yyou 中?提前致谢。

4

1 回答 1

0

据我所知,不可能在 C 中同时写入两个文件。但是,另一种方法是在写入第一个文件后对其进行复制。

FILE *fin=fopen(argv[1],"r");
FILE *output1 = fopen(argv[2], "w");
FILE *output2= fopen(argv[3], "w");
if(fin==NULL){
        printf("Cannot open specified file\n");
        return 0;
    }
    
// call the lexer with the first output file
yyin = fin;
yyout =output1;
yylex();

// reopen output file for input
fclose(output1);
output1 = fopen(argv[2]), "r");

// copy to the second file
while ((ch = fgetc(output1)) != EOF)
    fputc(ch, output2);

从C程序中提取复制文件,

于 2021-06-06T16:01:12.343 回答