|
Home > Archive > Unix Programming > February 2004 > non-blocking with fread or getc?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
non-blocking with fread or getc?
|
|
| Brian Genisio 2004-02-23, 1:34 pm |
| Hi all,
I am modifying code, that is using fopen / fread / getc as it's input
mechanisms for files. I am pretty sure I have gotten fread or getc to
act in a non-blocking way, but I simply cannot seem to remember how.
Really, I want to read if there is data available, and continue if not.
something like:
while(peek(file)) {
data = getc(file);
// do something with data
}
// no more data to read... continue
Unfortunately, I am blocking. What is the magic I need, so I can know
if data is available, or to read, with a return of nothing written, if
there is nothing? In a non-blocking way?
Thanks in advance,
Brian
| |
| Alexander Krisak 2004-02-23, 7:33 pm |
| Hello, "Brian Genisio" <BrianGenisio@nospam.yahoo.com>
> I am modifying code, that is using fopen / fread / getc as it's input
> mechanisms for files. I am pretty sure I have gotten fread or getc to
> act in a non-blocking way, but I simply cannot seem to remember how.
>
> Really, I want to read if there is data available, and continue if not.
>
> something like:
> while(peek(file)) {
> data = getc(file);
> // do something with data
> }
> // no more data to read... continue
>
> Unfortunately, I am blocking. What is the magic I need, so I can know
> if data is available, or to read, with a return of nothing written, if
> there is nothing? In a non-blocking way?
there is few variants to solve your problem
1. use low level functions open()/read()/write()/close() instead
fopen()/fread()/fwrite()/fclose() - there you can setup nonblocking
mode for the file.
2. if your program interact with terminal the ncurses library will help
you.
--
chris
| |
| Fletcher Glenn 2004-02-24, 3:34 am |
|
Brian Genisio wrote:
> Hi all,
>
> I am modifying code, that is using fopen / fread / getc as it's input
> mechanisms for files. I am pretty sure I have gotten fread or getc to
> act in a non-blocking way, but I simply cannot seem to remember how.
>
> Really, I want to read if there is data available, and continue if not.
>
> something like:
> while(peek(file)) {
> data = getc(file);
> // do something with data
> }
> // no more data to read... continue
>
> Unfortunately, I am blocking. What is the magic I need, so I can know
> if data is available, or to read, with a return of nothing written, if
> there is nothing? In a non-blocking way?
>
> Thanks in advance,
> Brian
>
If you're referring to reading individual characters from the keyboard
without requiring a carriage return, then you need to read the termios
man page about setting the terminal I/O mode. If you're referring to
non-blocking stdio, then you need to use fcntl() to set the O_NONBLOCK
flag on the file descriptor. I used this technique to create a
non-blocking getline() as a C++ class. It would return either NULL, or
a pointer to the new line of data, but it would not block.
--
Fletcher Glenn
| |
| Brian Genisio 2004-02-24, 4:34 am |
| Fletcher Glenn wrote:
>
> If you're referring to reading individual characters from the keyboard
> without requiring a carriage return, then you need to read the termios
> man page about setting the terminal I/O mode. If you're referring to
> non-blocking stdio, then you need to use fcntl() to set the O_NONBLOCK
> flag on the file descriptor. I used this technique to create a
> non-blocking getline() as a C++ class. It would return either NULL, or
> a pointer to the new line of data, but it would not block.
>
> --
>
> Fletcher Glenn
>
I am referring to the second option. Basically, I am making a
modification to allow reading data from a pipe for communication between
two programs. The program reading from the pipe cannot use things like
named pipes, due to portability issues... plus, I want to stick to the
conventions used in the rest of the code.
I will try fcntl when I get the chance... is it as safe to use for
portability as fopen/fread, etc?
Brian
|
|
|
|
|