Thread: popen and fgets problem

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    18

    Question popen and fgets problem

    I use popen to get the output of a process, and fgets to read a line. But if the process is still running, will I get buffer overflow (line)in the fgets?

    char [2048] line;
    FILE *fd;

    fd = popen("non-stop-program","r");
    while(fgets(line, 2048, fd)) {
    .. do things
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > will I get buffer overflow (line)in the fgets?
    No - fgets will never overflow the buffer you give it (unless you deliberately tell it the wrong size).

    It should behave as if you had redirected the output to a file, then fopen'ed that file for reading.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. popen and fgets, underflow!
    By henrikstolpe in forum Linux Programming
    Replies: 0
    Last Post: 02-06-2009, 03:39 AM
  2. Can not use popen()
    By sistem in forum Windows Programming
    Replies: 25
    Last Post: 10-26-2007, 02:43 AM
  3. popen and fgets
    By codez in forum C Programming
    Replies: 2
    Last Post: 01-07-2006, 11:14 AM
  4. popen help
    By ojschubert in forum C Programming
    Replies: 2
    Last Post: 02-28-2005, 12:32 AM