Using streams with FILE *

Using streams with FILE *

Post by Bj?rn Lindber » Wed, 20 Feb 2002 21:32:56



I'm writing a parser which will have two steps: (1) A preformatting
stage, and (2) a standard flex parser. For the preformatting I'm trying
to use streams, so I'm thinking of a function or class which takes a
stream (could be std::cin or an std::ifstream) and does the
preformatting.

Now, the problem is that for (2) I need to be able to send the data as a
C FILE * stream, beacuse that is what flex expects. So what format
should my preformatting step output the data in, and how do I convert it
to a FILE * structure?

I'm using the GNU g++ compiler.

Bjrn


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Using streams with FILE *

Post by Anthony Yue » Thu, 21 Feb 2002 05:18:27



> I'm writing a parser which will have two steps: (1) A preformatting
> stage, and (2) a standard flex parser. For the preformatting I'm trying
> to use streams, so I'm thinking of a function or class which takes a
> stream (could be std::cin or an std::ifstream) and does the
> preformatting.

> Now, the problem is that for (2) I need to be able to send the data as a
> C FILE * stream, beacuse that is what flex expects. So what format
> should my preformatting step output the data in, and how do I convert it
> to a FILE * structure?

> I'm using the GNU g++ compiler.

        You missed the thread that I started about mixing C and C++ I/O
(FILE * and iostream).  Basically you have two options:

        For step (1), you use a custom streambuf built around FILE *.

        or

        For step (1), you give up using iostream and just use FILE *.

        You cannot "convert" iostream to a FILE *.  That is the conclusion
of that thread.


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Using streams with FILE *

Post by Fraser Or » Thu, 21 Feb 2002 18:02:48



Quote:> Now, the problem is that for (2) I need to be able to send the data as a
> C FILE * stream, beacuse that is what flex expects. So what format
> should my preformatting step output the data in, and how do I convert it
> to a FILE * structure?

If you are preprocessing, I presume you store the results of your
preprocessing somewhere? If your write it back to file, simply
fopen it, otherwise, if you store it in memory you might consider
simply using flex's buffer routines, viz:

 // lexerInputString is a char* to the FILE read into memory.
 yy_switch_to_buffer(yy_scan_string(lexerInputString));

to start flex-ing, and:

 yy_delete_buffer(YY_CURRENT_BUFFER);

when you are done.

Alternatively, (although I have personally never done this) you can
redefine YY_INPUT to read from whatever you want. The format is
YY_INPUT(buf,result,max_size), and you can redefine it to read input
from wherever you want, streams, memory, FILE* or a random number
generator if you are so inclined. See the flex man page at:
<http://www.gsp.com/cgi-bin/man.cgi?section=1&topic=flex>

HTH.


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Using streams with FILE *

Post by James Kan » Fri, 22 Feb 2002 07:19:15





> > Now, the problem is that for (2) I need to be able to send the
> > data as a C FILE * stream, beacuse that is what flex expects. So
> > what format should my preformatting step output the data in, and
> > how do I convert it to a FILE * structure?

    [...]

Quote:> Alternatively, (although I have personally never done this) you can
> redefine YY_INPUT to read from whatever you want. The format is
> YY_INPUT(buf,result,max_size), and you can redefine it to read input
> from wherever you want, streams, memory, FILE* or a random number
> generator if you are so inclined. See the flex man page at:
> <http://www.gsp.com/cgi-bin/man.cgi?section=1&topic=flex>

More generally, you can pass the output of flex through a shell script
with sed to get what you want.  I've donne this for ages with lex and
yacc, to get C++ long before the actual tools had ever heard of the
language.  (I've also done it with yacc to change the error handling
strategy, but that's another story.)

But flex already has options for C++.  Just add:

    %option c++
    %option yyclass="..."

to the top of your flex source.  (Except that the version I have uses
a forward declaration of the *class* istream, which doesn't work with
modern libraries. So I still use a filter like:

    (
        echo "#include <istream>"
        cat $*
    ) | sed '
      /class istream;/d
      /^[^#]/s/istream/std::istream/
      /^[^#]/s/ostream/std::ostream/
      '

In my own code, the actual input is a MultipleFileIStream (which
derives from istream), and it seems to work.

--

Beratung in objektorientierer Datenverarbeitung --
                             -- Conseils en informatique oriente objet
Ziegelhttenweg 17a, 60598 Frankfurt, Germany, Tl.: +49 (0)69 19 86 27


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Using streams with FILE *

Post by Bj?rn Lindber » Fri, 22 Feb 2002 13:59:44



>         You missed the thread that I started about mixing C and C++ I/O
> (FILE * and iostream).  Basically you have two options:

>         For step (1), you use a custom streambuf built around FILE *.
>         or
>         For step (1), you give up using iostream and just use FILE *.

Thanks for the tip. I found the thread on google and read it all. I also
read an article about writing one's own streambuf classes that were
referenced in the thread.

I think that would be the nicest solution. It would be nice to have
stream classes & equivalent streambufs corresponding to eg sockets,
pipes etc.

The "syncbuf" class in that article seems rather inefficient though.
I'll have to read up on streambufs. Do you know about any other sources
of information on this, or examples of "home-made" streambufs?

Bj?rn


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Using streams with FILE *

Post by Bj?rn Lindber » Fri, 22 Feb 2002 14:00:21



> More generally, you can pass the output of flex through a shell script
> with sed to get what you want.  I've donne this for ages with lex and
> yacc, to get C++ long before the actual tools had ever heard of the
> language.  (I've also done it with yacc to change the error handling
> strategy, but that's another story.)

I'm not sure I understand this. What alterations would I make to the
flex-file?

Quote:> But flex already has options for C++.  Just add:

>     %option c++
>     %option yyclass="..."

I browsed this section of the manual, but really didn't like the
interface, so I decided to do without it. Also, I'm learning flex &
bison now and I want to learn the standard way to use them first.

The way I've solved it now is to make the preformatter a separate
program which is invoked by the main program via a popen() call. This
way I get a pipe to the output as a FILE *, which I then plan to make
flex use. In the preformatter program itself I'm using c++-streams for
all I/O, so you might say I side-stepped the problem for now.

Bj?rn


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

1. How to delete a Stream from a NTFS (streamed) file?

Hi!

Note: If you've never used a Streamed file on NTFS just skip this message!

I have a streamed file created with a normal 'saveto' statement using ADO
and Vbscript.

A streamed file can look like this

SomeFile.BIN:stream1.$DATA
SomeFile.BIN:stream2.$DATA
SomeFile.BIN:stream3.$DATA

etc
Now I want to delete stream2 with the Scripting.FileSystemObject
DeleteFile gives a invalid filename error!

This is really ridiculous... Because the Filesystemobject can read an rename
a file within the SAME base file but it can't delete!

Is there any workaround to perform a delete (for example) on stream2?

I have looked into the MSDN but it does not give a shot...

Regards,

Egbert

2. Can't do the spelling (Key F7)

3. Using IAMMultiMediaStream with non-file stream sources ...

4. Social Security Number FAQ

5. How can I record the audio stream to a file using tapi3.0 and vb

6. FOR ALL THE ONES WHO HATE ME!!!!!

7. HELP: Using C file stream I/O functions in Windows (QuickC)

8. Building my own custom motherboard...

9. BC4, using streams (which one!?) for file IO.

10. Using streams -vs- FTP for file transfer?

11. How to read data using streams from memory instead of files?

12. Writing double to file using C++ stream

13. Using stream class for > 2Gb files (redhat linux 7.3)