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 75221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:28:56+00:00 2026-05-10T20:28:56+00:00

I’m using the following code to try to read the results of a df

  • 0

I’m using the following code to try to read the results of a df command in Linux using popen.

#include <iostream> // file and std I/O functions  int main(int argc, char** argv) {     FILE* fp;     char * buffer;     long bufSize;     size_t ret_code;      fp = popen('df', 'r');     if(fp == NULL) { // head off errors reading the results         std::cerr << 'Could not execute command: df' << std::endl;         exit(1);     }      // get the size of the results     fseek(fp, 0, SEEK_END);     bufSize = ftell(fp);     rewind(fp);      // allocate the memory to contain the results     buffer = (char*)malloc( sizeof(char) * bufSize );     if(buffer == NULL) {         std::cerr << 'Memory error.' << std::endl;         exit(2);     }      // read the results into the buffer     ret_code = fread(buffer, 1, sizeof(buffer), fp);     if(ret_code != bufSize) {         std::cerr << 'Error reading output.' << std::endl;         exit(3);     }      // print the results     std::cout << buffer << std::endl;      // clean up     pclose(fp);     free(buffer);     return (EXIT_SUCCESS); } 

This code is giving me a ‘Memory error’ with an exit status of ‘2’, so I can see where it’s failing, I just don’t understand why.

I put this together from example code that I found on Ubuntu Forums and C++ Reference, so I’m not married to it. If anyone can suggest a better way to read the results of a system() call, I’m open to new ideas.

EDIT to the original: Okay, bufSize is coming up negative, and now I understand why. You can’t randomly access a pipe, as I naively tried to do.

I can’t be the first person to try to do this. Can someone give (or point me to) an example of how to read the results of a system() call into a variable in C++?

  • 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. 2026-05-10T20:28:57+00:00Added an answer on May 10, 2026 at 8:28 pm

    Why would std::malloc() fail?

    The obvious reason is ‘because std::ftell() returned a negative signed number, which was then treated as a huge unsigned number’.

    According to the documentation, std::ftell() returns -1 on failure. One obvious reason it would fail is that you cannot seek in a pipe or FIFO.

    There is no escape; you cannot know the length of the command output without reading it, and you can only read it once. You have to read it in chunks, either growing your buffer as needed or parsing on the fly.

    But, of course, you can simply avoid the whole issue by directly using the system call df probably uses to get its information: statvfs().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 78k
  • Answers 78k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Look at Math.Round(decimal) or the overload which takes a MidpointRounding… May 11, 2026 at 3:38 pm
  • added an answer We use a PowerShell script to run the wsdl.exe tool… May 11, 2026 at 3:38 pm
  • added an answer Bash supports programmable auto completion (at least since version 3.0).… May 11, 2026 at 3:38 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.