popen and fgets problem

This is a discussion on popen and fgets problem within the C Programming forums, part of the General Programming Boards category; I use popen to get the output of a process, and fgets to read a line. But if the process ...

  1. #1
    dkt
    dkt is offline
    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
    32,825
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21