1.C++实现ls命令
#include<dirent.h>
#include<stdlib.h>
#include<iostream>
#include "apue.h"
using namespace std;
int main(int argc,char * argv[]){
DIR *d
struct dirent *dirp;
if(argc!=2){
cout<<"usage: ls directory_name"<<endl;
exit(-1);
}
if((dp=opendir(argv[1]))==NULL){
cout<<"can't open "<<argv[1]<<endl;
}
//循环读取目录项
while((dirp=readdir(dp))!=NULL){
//输出文件名称
cout<<dirp->d_name<<endl;
}
closedir(dp);
exit(0);
}
opendir函数返回指向DIR结构的指针,我们将该指针传送给readdir函数。在循环中调用 readdir来读取每个目录项。该函数返回指向dirent结构的指针,而当目录中没有目录项可读时返回空指针。然后在循环中输出从dirent结构中 取出的每个目录项的名称。
2.C++ 实现从输出从标准输入中读到的内容
#include "apue.h"
#include<iostream>
using namespace std;
#define BUFFSIZE 4096
int main(){
int n;
//缓冲
char buf[BUFFSIZE];
//从标准输入中读取,缓冲区大小为BUFFSIZE,缓冲区对应buf
while((n=read(STDIN_FILENO,buf,BUFFSIZE))>0){
//向标准输出输出buf中的内容
if(write(STDOUT_FILENO,buf,n)!=n)
cout<<"wirte error"<<endl;
}
//读取标准输入出错
if(n<0)
cout<<"read error"<<endl;
exit(0);
}
运行截图:
3.使用标准I/O实现例2中的内容
标准I/O函数为那些不带缓冲的I/O函数提供了一个带缓冲的接口。使用标准I/O函数无需担心如何选取最佳的缓冲区大小。
以下用getc函数与putc函数实现例2中的功能。
#include "apue.h"
#include <iostream>
using namespace std;
int main(){
int c;
while((c=getc(stdin))!=EOF){
if(putc(c,stdout)==EOF)
cout<<"output error"<<endl;
}
if(ferror(stdin))
cout<<"input error"<<endl;
return 0;
}
4.输出进程ID
UNIX确保每个进程都有唯一的数字标识符,成为进程ID。进程ID是一个非负整数。
下面是一个输出本程序进程ID的 程序代码。
#include "apue.h"
#include <iostream>
using namespace std;
int main(){
cout<<"hello world from process ID"<<getpid()<<endl;
return 0;
}
分别运行两次,输出如下:
hello world from process ID18357
hello world from process ID18385
5.开辟新进程执行输入的命令
利用C++对标准输入 输入的命令开辟新的进程加以执行。
#include "apue.h"
#include <sys/wait.h>
#include <iostream>
using namespace std;
int main(){
char buf[MAXLINE]; //MAXLINE->4096
pid_t pid;
int status;
cout<<"%% ";/* print prompt (printf requires %% to print %)*/
while(fgets(buf,MAXLINE,stdin)!=NULL){
//fets函数返回的每一行都以换行符结束,后面紧随一个' ',需要将换行符号替换为' '
if(buf[strlen(buf)-1]=='
')//最后一个字符是换行符
buf[strlen(buf)-1]='