Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6688067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:21:41+00:00 2026-05-26T05:21:41+00:00

Does anyone know of a way to read in terminal output from bash commands

  • 0

Does anyone know of a way to read in terminal output from bash commands into c++? For example, entering “ls” into the terminal returns the file names in the current directory, is there any way to perhaps import these names into a string or char array or something? I have been looking at fork(), exec(), and pipe(). I was attempting to open a pipe communication between child (system commands entered here) and parent (output read in here) but have been thoroughly unsuccessful. Ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:21:42+00:00Added an answer on May 26, 2026 at 5:21 am

    Thanks Kerrek SB, I had seen people using popen() in various ways but wasn’t sure if that was the route to pursue or not. With the help of the documentation and putting together parts of how other people were using it, I came up with this. This does what the question I wrote asked about specifically: importing the terminal response from the command “ls” into a vector of file names. However, each file name contains a newline character that I implicitly remove pushing back all but the last element of the string file into the vector.

    void currentDirFiles( vector<string> & filesAddress )
    { 
        FILE * pipe = popen( "ls", "r" );
        char buffer[1000];
        string file;
        vector<string> files;
        while ( fgets( buffer, sizeof( buffer ), pipe ) != NULL )
        {
            file = buffer;
            files.push_back( file.substr( 0, file.size() - 1 ) );
        }
        pclose( pipe );
        filesAddress.swap( files );
    }
    

    I am trying to adapt the wikipedia RAII example to fit my needs as per the recommendations of Mankarse, and this is what I have so far. Does it look like I am on the right track? Any obvious mistakes or misunderstandings I have on this?

     #include <cstdio>
    
    // exceptions
    class file_error { } ;
    class open_error : public file_error { } ;
    class close_error : public file_error { } ;
    class read_error : public file_error { } ;
    
    class TerminalPipe
    {
       public:
           TerminalPipe():
               pipeHandle(std::popen("ls", "r"))
               {
                   if( pipeHandle == NULL )
                       throw open_error() ;
               } 
        ~TerminalPipe()
        {
            std::pclose(pipeHandle) ;
        } 
        void currentDirFiles( vector<string> & filesAddress )
        {
           char buffer[1000];
           string file;
           vector<string> files;
           while ( fgets( buffer, sizeof( buffer ), pipeHandle ) != NULL )
           {
               if( std::ferror( pipeHandle ) ) 
                   throw read_error();
               else
               {
                   file = buffer;
                   files.push_back( file.substr( 0, file.size() - 1 ) );
               }
           }
        }
        private:
            std::FILE* pipeHandle ;
            // copy and assignment not implemented; prevent their use by
            // declaring private.
            TerminalPipe( const file & ) ;
            TerminalPipe & operator=( const file & ) ;
    };
    

    Then call it with this:

    vector<string> dirFiles;
    TerminalPipe pipe;    
    pipe.currentDirFiles( dirFiles );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know a way to read email messages from a gmail account in
Does anyone know a way to get the mean amplitude of a .wav file
does anyone know of a way to format the date that is returned from
Does anyone know of a way to get numerous read-only git repositories based off
Does anyone know of a way to programmatically read the list of References in
Does anyone know a way get the pixel data from a PythonMagick.Image instance without
Does anyone know a way to register a persistent UDF in SQLite 3 (using
Does anyone know a way to get Firefox to crop the corners if the
Does anyone know a way to determine if a Rails association has been eager
Does anyone know of a way to contain a nonbreaking space in an html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.