Re: [Flex-help] flex and writing to file
flex is a tool for generating scanners
Brought to you by:
wlestes
From: Chris v. <che...@gm...> - 2010-08-11 03:47:02
|
fopen returns null on error, it's fprintf you have to check with "< 0". What about something like, void printRTfront(char * txt){ FILE *result = fopen("result","a+"); if (!result) { printf("EEK!\n"); return; } if (fprintf(result, "%s", txt) < 0) { printf("EEEK!\n"); return; } if (fclose(result) != 0) { printf("EEEEK!\n"); return; } } I'm not the biggest fan of C. :) -Chris On Tue, Aug 10, 2010 at 8:37 PM, Jeffrey Geyssens <jge...@gm...>wrote: > Well, > > I've checked and I am able to write to any other file but result. > I do write the result file before I want to write using flex. > This is most-likely the cause of the trouble. My C knowledge is > non-existent > (do have some C++ knowledge though). > > [0-9]+ { yylval = atoi(yytext); return NUMBER; } > > "<"[^>]*> { > char cwd[1024]; > if (getcwd(cwd, sizeof(cwd)) != NULL) > fprintf(stdout, "Current working dir: %s\n", cwd); > else > perror("getcwd() error"); > > FILE *r; > //r = fopen("result","a+"); > if (fopen("result", "a+") < 0) > fprintf(stderr, "Error: %s.\n", strerror(errno)); > else > printf("A OK"); > //printf("%i",errno); > //fprintf(r,"%s\n",yytext); > //fclose(r); > > printf("%s\n",yytext); > return COMMENT;} > > I get A OK so no errors.. > > The procedure which wrote result file beforehand is: > void printRTfront(char * txt){ > > FILE *result; > result = fopen("result","a+"); > fprintf(result,"%s", txt); > fclose(result); > } > > Must I flush something? > > 2010/8/11 Jeffrey Geyssens <jge...@gm...> > > > Well, > > > > I've checked and I am able to write to any other file but result. > > I do write the result file before I want to write using flex. > > This is most-likely the cause of the trouble. My C knowledge is > > non-existent (do have some C++ knowledge though). > > > > [0-9]+ { yylval = atoi(yytext); return NUMBER; } > > > > "<"[^>]*> { > > char cwd[1024]; > > if (getcwd(cwd, sizeof(cwd)) != NULL) > > fprintf(stdout, "Current working dir: %s\n", cwd); > > else > > perror("getcwd() error"); > > > > FILE *r; > > //r = fopen("result","a+"); > > if (fopen("result", "a+") < 0) > > fprintf(stderr, "Error: %s.\n", strerror(errno)); > > else > > printf("A OK"); > > //printf("%i",errno); > > //fprintf(r,"%s\n",yytext); > > //fclose(r); > > > > printf("%s\n",yytext); > > return COMMENT;} > > > > I get A OK so no errors.. > > > > The procedure which wrote result file beforehand is: > > void printRTfront(char * txt){ > > > > FILE *result; > > result = fopen("result","a+"); > > fprintf(result,"%s", txt); > > fclose(result); > > } > > > > Must I flush something? > > > > > > 2010/8/11 Chris verBurg <che...@gm...> > > > > > >> Did you check the error code on fopen? Maybe you're out of file > handles, > >> or the disk is full, or you don't have write permissions, or blah blah > blah. > >> > >> -Chris > >> > >> > >> On Tue, Aug 10, 2010 at 7:54 PM, Jeffrey Geyssens <jge...@gm... > >wrote: > >> > >>> Hello all, > >>> > >>> I have a noobish flex question, which has been consuming far to much > >>> time. > >>> I figured time to ask the experts. > >>> > >>> I have a flex file which contains the following rule: > >>> > >>> #include "stdio.h" > >>> #include "yygrammar.h" > >>> #include "unistd.h" > >>> > >>> "<"[^>]*> { > >>> char cwd[1024]; > >>> if (getcwd(cwd, sizeof(cwd)) != NULL) > >>> fprintf(stdout, "Current working dir: %s\n", cwd); > >>> else > >>> perror("getcwd() error"); > >>> > >>> FILE *result; > >>> result = fopen("result","a+"); > >>> fprintf(result,"%s\n",yytext); > >>> fclose(result); > >>> printf("%s\n",yytext); > >>> return COMMENT;} > >>> > >>> I want to print the working directory (to see where i am) and then > write > >>> yytext to a file. > >>> The working directory is correct, and I assure you that result file is > >>> located in this directory. > >>> > >>> I can print yytext to the screen using printf just fine. > >>> > >>> But I can't manage to print the yytext to the result file. The code is > >>> copied from another part of the program, which works fine. > >>> I'm starting to get scared... Please help > >>> > >>> > ------------------------------------------------------------------------------ > >>> This SF.net email is sponsored by > >>> > >>> Make an app they can't live without > >>> Enter the BlackBerry Developer Challenge > >>> http://p.sf.net/sfu/RIM-dev2dev > >>> _______________________________________________ > >>> Flex-help mailing list > >>> Fle...@li... > >>> https://lists.sourceforge.net/lists/listinfo/flex-help > >>> > >> > >> > > > > > > -- > > met beleefde groeten > > Jeffrey Geyssens > > > > > > -- > met beleefde groeten > Jeffrey Geyssens > > ------------------------------------------------------------------------------ > This SF.net email is sponsored by > > Make an app they can't live without > Enter the BlackBerry Developer Challenge > http://p.sf.net/sfu/RIM-dev2dev > _______________________________________________ > Flex-help mailing list > Fle...@li... > https://lists.sourceforge.net/lists/listinfo/flex-help > |