#include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<string.h> #define BUFSIZE 200 int main() { FILE *read_fp; char buffer[BUFSIZE+1];/*用于存放读取的内容*/ int chars_read; memset(buffer,'\0',sizeof(buffer)); read_fp=popen("uname -a","r");//用于显示内核版本 及现在时间 /*返回的是文件指针,因此-文件指针是要定义的*/ if(read_fp != NULL) { chars_read=fread(buffer,sizeof(char),BUFSIZE,read_fp);//返回读取字符的个数,错误则为-1 /*并读取传回的内容是什么*/ if(chars_read > 0) { printf("Output was:-\n%s\n",buffer); } pclose(read_fp); exit(EXIT_SUCCESS); } exit(EXIT_FAILURE); } /*因些,使用popen到少要定义: 文件指针, 字符串 */
#include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<string.h> #define BUFSIZE 50 int main() { FILE *read_fp; char buffer[BUFSIZE+1];/*用于存放读取的内容*/ int chars_read; memset(buffer,'\0',sizeof(buffer)); read_fp=popen("ps ax","r");//用于显示内核版本 及现在时间 /*返回的是文件指针,因此-文件指针是要定义的*/ if(read_fp != NULL) { while(chars_read > 0) { //返回读取字符的个数,错误则为-1 /*并读取传回的内容是什么*/ chars_read=fread(buffer,sizeof(char),BUFSIZE,read_fp); buffer[chars_read-1]='\0'; printf("Reading %d:-\n %s \n",BUFSIZE,buffer); } pclose(read_fp); exit(EXIT_SUCCESS); } exit(EXIT_FAILURE); return 0; } /*因些,使用popen到少要定义: 文件指针, 字符串 */
#include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<string.h> #define BUFSIZE 50 int main() { FILE *read_fp; char buffer[BUFSIZE+1];/*用于存放读取的内容*/ int chars_read; memset(buffer,'\0',sizeof(buffer)); read_fp=popen("cat 13-3-*.cpp | wc -l","r");//用于显示内核版本 及现在时间 /*返回的是文件指针,因此-文件指针是要定义的*/ if(read_fp != NULL) { while(chars_read > 0) { //返回读取字符的个数,错误则为-1 /*并读取传回的内容是什么*/ chars_read=fread(buffer,sizeof(char),BUFSIZE,read_fp); buffer[chars_read-1]='\0'; printf("Reading %d:-\n %s \n",BUFSIZE,buffer); } pclose(read_fp); exit(EXIT_SUCCESS); } exit(EXIT_FAILURE); return 0; } /*因些,使用popen到少要定义: 文件指针, 字符串 */
#include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<string.h> #define Buffer 100 int main() { FILE *write_fp; char buffer[Buffer + 1]; //把格式化的数据写入某个字符串缓冲区。 //返回值:字符串长度(strlen) sprintf(buffer,"Once upon a time, there was...\n"); //********格式化输出文件中的数据***********/// //-c:等价于-t c,选择ASCII码字符或者是转义字符 write_fp=popen("od -c","w"); if(write_fp != NULL) { fwrite(buffer,sizeof(char),strlen(buffer),write_fp); pclose(write_fp); exit(EXIT_SUCCESS); } exit(EXIT_FAILURE); }
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> int main() { int data_processed; int file_pies[2]; const char some_data[]="123"; char buffer[BUFSIZ+1]; memset(buffer,'\0',sizeof(buffer)); if(pipe(file_pies)==0) { data_processed=write(file_pies[1],some_data,strlen(some_data)); printf("Wrote %d bytes\n",data_processed); data_processed=read(file_pies[0],buffer,BUFSIZ); printf("Read %d bytes: %s\n",data_processed,buffer); exit(EXIT_SUCCESS); } exit(EXIT_FAILURE); }