Re: Redirect stdin ?



On Dec 31, 1:40 pm, Lothar Behrens <lothar.behr...@xxxxxxxxxxxx>
wrote:

I have a c application (Lex&Yacc based) that parses from stdin. I want
to rewrite the code
a bit to allow reading the source from a char array instead from stdin
without touching the
lex and yacc generated source code.

How could I do that ?

What is about the problem when the buffer is too big to feed the
source to the parser at once ?

Any ideas ?

If you are okay with using gcc extensions, here's a section
from the glibc info pages you might find interesting:

Here is an example of using `fmemopen' to create a stream for
reading from a string:

#include <stdio.h>

static char buffer[] = "foobar";

int
main (void)
{
int ch;
FILE *stream;

stream = fmemopen (buffer, strlen (buffer), "r");
while ((ch = fgetc (stream)) != EOF)
printf ("Got %c\n", ch);
fclose (stream);

return 0;
}

This program produces the following output:

Got f
Got o
Got o
Got b
Got a
Got r
.



Relevant Pages

  • Re: question
    ... fgets writes to stdin maybe that's what I'm thinking of. ... fgetsreads from a text stream opened for reading. ... If vfprintf is like fprintf ...
    (comp.lang.c)
  • Re: question
    ... fgets writes to stdin maybe that's what I'm thinking of. ... fgetsreads from a text stream opened for reading. ... advice was given to me not to consider the functions begining with f ...
    (comp.lang.c)
  • Re: segmentation fault with gets()
    ... > provide an alternative using scanf or fgets(without reading a file, ... int chop ... if(fgets(buf, sizeof buf, stdin)!= NULL) ...
    (comp.lang.c)
  • Re: fread()
    ... > int i,istat,iaccum,intype; ... reading binary data from standard input? ... Use sizeof (float), or even better, sizeof *floatarray, instead. ... FLOAT, and set istat accordingly? ...
    (comp.lang.c)
  • Re: Reading Simple Flatfiles with a COBOL Mindset
    ... I am new to C and old to COBOL ... > in with respect to reading a simple file - one record at a time. ... stream and impose your interpretation on them: ... there are text streams and binary streams. ...
    (comp.lang.c)