473,396 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

fgetc() past EOF


I'm writing a little BrainFuck interpreter (by the way, gbf.sourceforge.net)
that can read the BrainFuck program from stdin; when the interpreter gets an
EOF, it stops reading the BrainFuck program and starts interpreting it;
BrainFuck has i/o facilities, and I use stdin/stdout to receive/spit out the
BrainFuck fluff. The problem is that stdin seems to lock after fgetc() gets
an EOF from it, and doesn't reset anymore. If I get the BrainFuck program
text from a file, it all works correctly, but if I read the text from stdin,
get an EOF, and keep on trying to read from it, the interpreter hangs. How
can I reset the EOF status (if there is such a thing) of stdin? Or is there
a workaround for this problem?

If you want to test-drive it, please mail me.

--
Emacs is a great operating system---it lacks a good editor, though.

Nov 14 '05 #1
6 3297
José de Paula wrote:
I'm writing a little BrainFuck interpreter (by the way, gbf.sourceforge.net)
that can read the BrainFuck program from stdin; when the interpreter gets an
EOF, it stops reading the BrainFuck program and starts interpreting it;
BrainFuck has i/o facilities, and I use stdin/stdout to receive/spit out the
BrainFuck fluff. The problem is that stdin seems to lock after fgetc() gets
an EOF from it, and doesn't reset anymore.
Of course. EOF means the file ended. It's not part of the input
stream.
How
can I reset the EOF status (if there is such a thing) of stdin? Or is there
a workaround for this problem?


clearerr(stdin) works on my system (Solaris), but I don't know how
portable it is. You should ask on a newsgroup for the OS and C
implementation you are using. Maybe a safer way would be to re-open the
terminal, e.g. freopen("/dev/tty", "r", stdin) if you are using Unix.
However, I'm not sure if that breaks input from programs that use PTYs,
like expect. Anyway, that belongs on a Unix group.

--
Hallvard
Nov 14 '05 #2
José de Paula <jo***********@ig.com.br> writes:
I'm writing a little BrainFuck interpreter (by the way, gbf.sourceforge.net)
that can read the BrainFuck program from stdin; when the interpreter gets an
EOF, it stops reading the BrainFuck program and starts interpreting it;
BrainFuck has i/o facilities, and I use stdin/stdout to receive/spit out the
BrainFuck fluff. The problem is that stdin seems to lock after fgetc() gets
an EOF from it, and doesn't reset anymore. If I get the BrainFuck program
text from a file, it all works correctly, but if I read the text from stdin,
get an EOF, and keep on trying to read from it, the interpreter hangs. How
can I reset the EOF status (if there is such a thing) of stdin? Or is there
a workaround for this problem?


The behavior in the face of end-of-file on stdin varies among C
libraries. Some behave as you describe; others will happily
continue to read after sending end-of-file once. There is no
standard way to "reset" one that has the former behavior.

I recommend reading the program from a different file, perhaps
one whose name is specified on the command line. This would
align your interpreter's behavior with many others, such as Perl,
Python, sed, the Bourne shell, and so on. It would also make it
possible to make programs written in the language invocable on
Unix-like command lines using the standard Unix #! syntax.
--
"Given that computing power increases exponentially with time,
algorithms with exponential or better O-notations
are actually linear with a large constant."
--Mike Lee
Nov 14 '05 #3
Em Sat, 17 Jan 2004 07:57:57 +0100, Hallvard B Furuseth escreveu:
<snip>
clearerr(stdin) works on my system (Solaris), but I don't know how
portable it is. You should ask on a newsgroup for the OS and C
implementation you are using. Maybe a safer way would be to re-open the
terminal, e.g. freopen("/dev/tty", "r", stdin) if you are using Unix.
However, I'm not sure if that breaks input from programs that use PTYs,
like expect. Anyway, that belongs on a Unix group.


Thank you. freopen() worked for me; clearerr() didn't. I forgot to
mention that my system is a FreeBSD on a i386, gcc 3.2 (but still dreaming
with a IBM Power IV...)

--
Emacs is a great operating system---it lacks a good editor, though.

Nov 14 '05 #4
>José de Paula <jo***********@ig.com.br> writes:
... How can I reset the EOF status (if there is such a thing)
of stdin?

In article <news:87************@pfaff.stanford.edu>
Ben Pfaff <bl*@cs.stanford.edu> writes:The behavior in the face of end-of-file on stdin varies among C
libraries. Some behave as you describe; others will happily
continue to read after sending end-of-file once. There is no
standard way to "reset" one that has the former behavior.


Well, clearerr() is standard and will (must) reset the EOF indicator
(so that feof(stdin) becomes false) along with the error indicator.
But it is true that there is no guarantee that a subsequent getchar()
will even try to read more input. On some systems I suspect it
would not without OS assistance -- e.g., on EXEC-8 on a Univac,
@FIN on a line by itself is the method one uses to signal end-of-file,
but it likely works by marking something the C runtime would not
have access to, since @ lines bypass just about everything except
the host OS (@@ lines apparently bypass the OS too -- @@TERM seems
to shut off the connection at the concentrator).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #5
José de Paula wrote:
Hallvard B Furuseth escreveu:

<snip>
clearerr(stdin) works on my system (Solaris), but I don't know
how portable it is. You should ask on a newsgroup for the OS
and C implementation you are using. Maybe a safer way would be
to re-open the terminal, e.g. freopen("/dev/tty", "r", stdin)
if you are using Unix. However, I'm not sure if that breaks
input from programs that use PTYs, like expect. Anyway, that

belongs on a Unix group.

Thank you. freopen() worked for me; clearerr() didn't. I forgot
to mention that my system is a FreeBSD on a i386, gcc 3.2 (but
still dreaming with a IBM Power IV...)


Fine if it is only for your own amusement. The only clean and
portable way is to separate the files, IMO.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #6
AM
Chris Torek <no****@torek.net> wrote in message news:<bu*********@enews4.newsguy.com>...

Well, clearerr() is standard and will (must) reset the EOF indicator
(so that feof(stdin) becomes false) along with the error indicator.
But it is true that there is no guarantee that a subsequent getchar()
will even try to read more input. On some systems I suspect it
would not without OS assistance -- e.g., on EXEC-8 on a Univac,
@FIN on a line by itself is the method one uses to signal end-of-file,
but it likely works by marking something the C runtime would not
have access to, since @ lines bypass just about everything except
the host OS (@@ lines apparently bypass the OS too -- @@TERM seems
to shut off the connection at the concentrator).

Not quite. @FIN was (is?) the end of job card. End of file was @EOF.
In practice, input to a processor - compiler, linker, assembler - was
terminated by the @ character in column 1.

The @@ commands were obeyed out of sequence - "transparent mode". In
demand mode processing, if you had a number of commands waiting to be
executed, you could send e.g., @@TERM, and it would be obeyed
essentially instantaneously by the executive. I forget the @@ comands,
but ISTR they included ASG (mass storage assignment), FREE (release
mass storage back to the system) and SYM - send a print or punch file
to the appropriate device. It's all a bit hazy now since it's 20yrs
since I last used a Univac...

rgds A
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: William L. Bahn | last post by:
I'm sure this has been asked before, and I have looked in the FAQ, but I'm looking for an explanation for the following: The functions pairs: gets()/fgets() puts()/fputs() printf()/fprintf()...
6
by: Kobu | last post by:
Do the "larger" input functions like scanf, gets, fgets use fgetc to take input or an operating system call function like read() (I know it could be any "way", but I'm trying to find out how it's...
2
by: Mark | last post by:
i'm trying to read a file one char at a time into a char array thusly... char buffer; while (readChars(InputFile, buffer, BUFFER_SIZE) != 0) { //not doing anything atm } int readChars(FILE...
8
by: M. Åhman | last post by:
I'm reading "C: A Reference Manual" but still can't understand a very basic thing: is there any functional difference between fgetc/fputc and fread/fwrite (when reading/writing one unsigned char)?...
3
by: cinsky | last post by:
Hi, While reading ISO C Standard, I found follow text in 7.19.8.1: size_t fread(void *restrict ptr, size_t SIZE, ...) ... For each object, SIZE calls are made to the fgetc function and the...
37
by: 01 | last post by:
#include <stdio.h> int main ( int argc, char *argv ) { if ( argc != 2 ) /* argc should be 2 for correct execution */ { /* We print argv assuming it is the program name */ printf( "usage: %s...
4
by: Christopher Benson-Manica | last post by:
In a thread from substantially earlier this week, Harald van D?k <truedfx@gmail.comwrote: Being rather pendantic, I decided to try to verify whether this was true. I would appreciate...
9
by: primeSo | last post by:
// FIRST int main(void){ int c, i = 0; char cArray; while( (c = getchar()) != '\n' && c != EOF){ cArray = c; i ++; }
2
by: Praesidium | last post by:
I'm having a bit of a problem running a C program I'm working on (compiled in cygwin). There's a way to go, as I'm intent on making sure each bit works before moving on to the next. As it appears...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.