How do I read the results of a system() call in C++?

后端 未结 9 940
醉梦人生
醉梦人生 2020-12-17 06:44

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

#include  // file an         


        
9条回答
  •  孤城傲影
    2020-12-17 07:27

    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().

提交回复
热议问题