The function prototype :

  #include “stdio.h”

  FILE *popen( const char* command, const char* mode )

   Parameter description :

  command: It's a point to NULL The end of the shell Pointer to the command string . This command will be sent to bin/sh And use -c sign ,shell This command will be executed .

  mode: It can only be one of reading or writing , The return value ( standard I/O flow ) It also has and type The corresponding read-only or write only type . If type yes “r” Then the file pointer is connected to command Standard output of ; If type yes “w” Then the file pointer is connected to command Standard input for .

   Return value :

   If the call succeeds , Returns a pointer to read or open the file , If you fail , return NULL, Specific mistakes should be based on errno Judge

  int pclose (FILE* stream)

   Parameter description :

  stream:popen File pointer returned

   Return value :

   If the call fails , return -1

   effect :

  popen() Function to create a pipe : Its internal implementation is to call fork Generate a subprocess , Execute one shell To start a process by running a command. The process must be started by pclose() Function off .

   Example :

   Pipeline reading : Create a file first test, And then again test Write in file “Read pipe successfully !”

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(void)
  4. {
  5. FILE *fp;
  6. char buf[1024] =   { 0 };
  7. if ((fp = popen("cat test", "r")) == NULL)
  8. {
  9. perror("Fail to popen\n");
  10. exit(1);
  11. }
  12. while (fgets(buf, 200, fp) != NULL)
  13. {
  14. printf("%s\n",buf);
  15. }
  16. pclose(fp);
  17. return EXIT_SUCCESS;
  18. }

The pipeline writes :

  1. #include “stdio.h”
  2.   #include “stdlib.h”
  3.   int main()
  4.   {
  5.   FILE *fp;
  6.   char buf[200] = {0};
  7.   if((fp = popen(“cat > test1″, “w”)) == NULL) {
  8.   perror(“Fail to popen\n”);
  9.   exit(1);
  10.   }
  11.   fwrite(“Read pipe successfully !”, 1, sizeof(“Read pipe successfully !”), fp);
  12.   pclose(fp);
  13.   return 0;
  14.   }

After execution , There is one more in the current directory test1 file , open , It's about Read pipe successfully !

Reprinted from :http://blog.csdn.net/ningxiaowei2013/article/details/25819077

popen() Use examples ( Reprint ) More articles about

  1. Linux A firewall iptables Learning notes ( 3、 ... and )iptables Command details and examples [ Reprint ]

     Linux A firewall iptables Learning notes ( 3、 ... and )iptables Command details and examples  2008-10-16 23:45:46 Reprint This configuration is easy to understand when I see it on the Internet , So it turned around , Let's see , I hope to be able to ...

  2. listener.ora--sqlnet.ora--tnsnames.ora And manual configuration ( Reprint :http://blog.chinaunix.net/uid-83572-id-5510.ht)

    listener.ora--sqlnet.ora--tnsnames.ora And manual configuration ==================== Recently I saw a lot of people say tns Or the database can't log in , Just sum up ...

  3. UML Class diagram symbols Description and examples of various relationships ( Reprint )

    The article citations :http://www.cnblogs.com/duanxz/archive/2012/06/13/2547801.html UML The ways to describe the relationships between objects and classes in include : rely on (Dependen ...

  4. 【 Reprint 】 stay LoadRunner To execute a command line program in :popen() replace system()

    I think you all know that in LoadRunner You can use functions system() To call system instructions , The result is the same as execution in batch . however system() There's a flaw : Unable to get the return result of the command . Maybe you can use `echo comm ...

  5. C stat Examples of function usage ( Reprint )

    stat Function to explain the header file :    #include <sys/stat.h>             #include <unistd.h> Defined function :    int stat( ...

  6. Reprint SSIS Containers and data flows in — Give an example to illustrate the data conversion task

    In the last essay, we were familiar with the data flow task , Now let's take an example , Learn the content of these introductions through practice . This example starts with AdventureWorks Get data from the database , And then aggregate the data , Sort , The calculation generates a new column operation and inputs it into a .csv ...

  7. Android-- ArrayAdapter Usage example ( Reprint )

    A lot of these days Android The developer wrote to say that he was right ArrayAdapter and BaseAdapter The difference is not very clear , here Android123 Let's talk about their relationship and use ,ArrayAdapter It's from BaseAdapt ...

  8. Android Control ——7 Species form Android Dialog Use examples ( Reprint )

    stay Android In development , We often need to be in Android Some dialog boxes pop up on the interface , For example, ask the user or let the user choose . We call these functions Android Dialog Dialog box , When we use Android In the process of , I summed up a ...

  9. 【 Reprint 】linux in shell command test Usage and examples

    test The shortest definition of a command might be to evaluate an expression : If the condition is true , Returns a 0 value . If the expression is not true , Return a value greater than 0 Value — It can also be called a false value . The easiest way to check the status of the last executed command is to use $? value ...

Random recommendation

  1. IIS Configure automatic login for domain users

    1. First determine IIS Whether your computer has been added to the domain : Right click on the computer -> attribute , In the computer name , Domain , The domain of the computer can be seen in the workgroup settings , If there is no , You can click change settings , Then click Change , Just select the domain to bind to ( You need to log in with a domain account ). ...

  2. One a day linux command (18):locate command

    locate  Allows users to quickly search the file system for specific files . The method is to set up a database including all the file names and paths in the system , Then, when searching, just query the database , Instead of actually going deep into the file system . In a normal  di ...

  3. Use javascript and canvas Half moon

    Use javascript and canvas Half moon , Half moon is so romantic ! Browser must support html5 See the effect :http://keleyi.com/a/bjad/8xqdm0r2.htm Here's the code : <!do ...

  4. Notes on Liao Xuefeng's course :js in map and reduce Usage of

    Illustrate with examples , For example, we have a function f(x)=x2, To apply this function to an array [1, 2, 3, 4, 5, 6, 7, 8, 9] On , You can use it map The implementation is as follows : because map() Method defined in JavaScript Of ...

  5. Spring Single instance 、 Multithreading security 、 Transaction resolution

    original text :http://blog.csdn.net/c289054531/article/details/9196053 introduction :     In the use of Spring when , Many people may be right Spring Why DAO and Se ...

  6. hduoj 4715 Difference Between Primes 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Time Limit: 2000/1000 MS (J ...

  7. nginx url rewrite rewrite example

    In this article , stay nginx To realize Url rewrite , Study rewrite Specific usage of , Friends in need can refer to it . Original address :http://www.360doc.com/content/14/0202/20/142341 ...

  8. Layout reuse include merge ViewStub

    In layout optimization ,Androi The official mentioned these three layouts <include />.<merge />.<ViewStub />, The advantages of these three layouts are introduced , Here is a brief introduction ...

  9. CSS3 @font-face Using examples

    Windows10 Examples of operating system usage : 1. Prepare font library of corresponding format 2. Examples of using Founder font <!DOCTYPE html> <html> <head> <meta ...

  10. malloc and free The difference between

    1,malloc And free yes C++/C Standard library functions for languages ,new/delete yes C++ Operator . They can be used to request dynamic memory and free memory . 2, For objects that are not internal data types , Light use maloc/free Can't satisfy ...