Reading console output and writing to console

Discussion in 'C++' started by jan.rebada@gmail.com, Jan 26, 2008.

  1. Guest

    Hello,

    I have a problem. I'm creating a program that uses system. Say for
    example I invoke system("mysql -uroot -p"), the next line in the
    screen will be enter password>, what I want with my C++ program is to
    detect if the screen reads 'enter password>' and automatically my
    program will input the appropriate password.

    Any help will do. Thanks.
     
    , Jan 26, 2008
    #1
    1. Advertising

  2. Rolf Magnus Guest

    wrote:

    > Hello,
    >
    > I have a problem. I'm creating a program that uses system. Say for
    > example I invoke system("mysql -uroot -p"), the next line in the
    > screen will be enter password>, what I want with my C++ program is to
    > detect if the screen reads 'enter password>' and automatically my
    > program will input the appropriate password.


    Not possible with the system() function. You need to use operating system
    specific functions to do this, e.g. the POSIX function popen.
     
    Rolf Magnus, Jan 26, 2008
    #2
    1. Advertising

  3. FNX Guest

    Around about 26/01/08 07:39, Rolf Magnus scribbled ...
    > Not possible with the system() function. You need to use operating system
    > specific functions to do this, e.g. the POSIX function popen.


    Unfortunately, popen() only allows reading *or* writing to the child
    process, not both. There's no really straightforward way; try reading:

    http://www.developerweb.net/forum/showthread.php?t=3026


    Certainly raw C++ as it stands has nothing like this, although Boost
    or Loki might.

    --
    [~]# sed -e 's/\.no\.junk\.please//g' .signature > .signature+
    [~]# rm -f .signature
    [~]# ls -l .signature
    ls: .signature: No such file or directory
     
    FNX, Jan 26, 2008
    #3
  4. James Kanze Guest

    On Jan 26, 9:11 am, FNX <> wrote:
    > Around about 26/01/08 07:39, Rolf Magnus scribbled ...


    > > Not possible with the system() function. You need to use operating system
    > > specific functions to do this, e.g. the POSIX function popen.


    > Unfortunately, popen() only allows reading *or* writing to the child
    > process, not both. There's no really straightforward way; try reading:


    > http://www.developerweb.net/forum/showthread.php?t=3026


    > Certainly raw C++ as it stands has nothing like this, although Boost
    > or Loki might.


    Note that using a pipe in both directions to a single process is
    not a good idea---you probably want two pipes. And even then,
    be very, very careful about deadlock. A pipe can only hold so
    much, and once it's full, a write blocks.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jan 26, 2008
    #4
  5. Guest

    James:

    I have exactly the deadlock problem you have just described.

    I have two pipes for reading and writing. After 20KB the pipe buffer
    is full. Every reading from the pipe returns errno 11 (EAGAIN), and
    the child process stop writing due to the buffer overflow.

    The parent keeps waiting for the son to finish, while the son keeps
    waiting for the parent to empty the buffer.

    How can I solve this?

    Fabio A. Mazzarino

    James Kanze escreveu:

    > On Jan 26, 9:11 am, FNX <> wrote:
    > > Around about 26/01/08 07:39, Rolf Magnus scribbled ...

    >
    > > > Not possible with the system() function. You need to use operating system
    > > > specific functions to do this, e.g. the POSIX function popen.

    >
    > > Unfortunately, popen() only allows reading *or* writing to the child
    > > process, not both. There's no really straightforward way; try reading:

    >
    > > http://www.developerweb.net/forum/showthread.php?t=3026

    >
    > > Certainly raw C++ as it stands has nothing like this, although Boost
    > > or Loki might.

    >
    > Note that using a pipe in both directions to a single process is
    > not a good idea---you probably want two pipes. And even then,
    > be very, very careful about deadlock. A pipe can only hold so
    > much, and once it's full, a write blocks.
    >
    > --
    > James Kanze (GABI Software) email:
    > Conseils en informatique orient�e objet/
    > Beratung in objektorientierter Datenverarbeitung
    > 9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
     
    , Mar 3, 2008
    #5
  6. James Kanze Guest

    On Mar 3, 6:18 pm, ""
    <> wrote:

    > I have exactly the deadlock problem you have just described.


    > I have two pipes for reading and writing. After 20KB the pipe buffer
    > is full. Every reading from the pipe returns errno 11 (EAGAIN), and
    > the child process stop writing due to the buffer overflow.


    > The parent keeps waiting for the son to finish, while the son keeps
    > waiting for the parent to empty the buffer.


    > How can I solve this?


    There's no general solution. The system will only store so much
    information for you in a pipe. One solution I've used in
    specific cases is for one of the processes to write to a
    temporary file, rather than the pipe, and only dump the
    temporary file to the pipe when it has finished. This supposes,
    however, that "finished" has some meaning here. More generally,
    beyond a certain limit, you'll have to shift to using files,
    rather than pipes; if a process is sending very big chunks, you
    might consider putting each chunk in a separate file, and only
    piping the file name. (When the receiving process has finished
    with the data, it can then delete the file.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Mar 4, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -
    Replies:
    1
    Views:
    397
    Gordon Beaton
    Mar 21, 2005
  2. lec
    Replies:
    1
    Views:
    407
    Grant Edwards
    Jul 17, 2003
  3. Miktor
    Replies:
    7
    Views:
    12,126
    Joe Bacigalupa
    Aug 9, 2005
  4. pratap
    Replies:
    3
    Views:
    472
    James Kanze
    Jul 19, 2007
  5. Mav
    Replies:
    22
    Views:
    382
Loading...

Share This Page