Professional Web Applications Themes

non-blocking with fread or getc? - UNIX Programming

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 ...

  1. #1

    Default non-blocking with fread or getc?

    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

    Brian Guest

  2. #2

    Default Re: non-blocking with fread or getc?

    Hello, "Brian Genisio" <>
     

    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



    Alexander Guest

  3. #3

    Default Re: non-blocking with fread or getc?



    Brian Genisio 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

    Fletcher Guest

  4. #4

    Default Re: non-blocking with fread or getc?

    Fletcher Glenn wrote:
     

    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

    Brian Guest

Similar Threads

  1. #39423 [NEW]: fread expired
    By base5 at 126 dot com in forum PHP Bugs
    Replies: 1
    Last Post: November 8th, 01:43 PM
  2. Linebreaks and fread
    By Eric Anderson in forum PHP Development
    Replies: 3
    Last Post: February 16th, 07:00 PM
  3. Will Thread blocking in read() leads to process blocking?
    By Loic Domaigne in forum UNIX Programming
    Replies: 4
    Last Post: July 23rd, 12:36 PM
  4. fread() question
    By Seairth Jacobs in forum PHP Development
    Replies: 1
    Last Post: July 12th, 03:27 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139