UNIX环境高级编程学习笔记

图1-3代码


#include <sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<dirent.h>


int main(int argc,char *argv[])
{
        DIR *dp;
        struct dirent *dirp;
        if(argc==2)
        printf("usage:ls directory_name");
        if((dp = opendir(argv[1]))==NULL)
        {
                printf("can't open %s",argv[1]);
        }
//              err_sys("can't open %s",argv[1]);
        while((dirp=readdir(dp))!=NULL)
                printf("%s\n",dirp->d_name);
        closedir(dp);
        exit(0);
}
  1. 说明
    ls 命令简要实现。没有使用作者自带的“aque.h”头文件
  2. 问题
    有ctags 跳转 opendir 找不到,readdir 跳转的内容也不对
  3. 运行命令“./1-3.out ~ “结果
    c
    .Xauthority
    .ICEauthority
    .
    redis
    .xsession-errors
    .local
    文档
    .ssh
    .bash_logout
    图片
    .gnuplot_history
    .profile
    .presage
    .bashrc
    公共的
    模板
    .cache
    .swp
    .gnupg
    .session
    .viminfo
    .xsession-errors.old
    .vnc
    .sudo_as_admin_successful
    .xinputrc
    .remmina
    视频
    .vimrc~
    .config
    .xsession
    ..
    桌面
    examples.desktop
    下载
    音乐
    .gdbinit
    .dmrc
    learning_code
    .mozilla
    .bash_history
    Firefox_wallpaper.png
    .vimrc
    .dbus
    .vim

图1-4代码

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#define BUFFERSIZE 4096
int main(void)
{
        int n;
        char buf[BUFFERSIZE];
        while((n=read(STDIN_FILENO,buf,BUFFERSIZE))>0)
        {
                if(write(STDOUT_FILENO,buf,n)!=n)
                {
                        printf("write error");
                }

        }
        if(n<0)
        {
                printf("read error");
        }
        exit(0);
}
  1. 说明
    从命令行或文件读入内容输入到指定文件,按块读入
  2. 问题
    暂无
  3. 运行命令 “./1-4.out >data”或“./1-4.out < data >outdata”

图1-5代码

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
        int c;
        while((c=getc(stdin))!=EOF)
        {
                if(putc(c,stdout)==EOF)
                {
                        printf("output error");
                }
        }
        if(ferror(stdin))
                printf("inout error");
        exit(0);
}
  1. 说明
    从命令行读入内容输入到指定文件,按字符读入
  2. 问题
    暂无
  3. 运行命令 “./1-5.out >data”或“./1-4.out < data >outdata”

图1-5代码

#include <sys/types.h>
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
        printf(" currnet process id is %d",(long)getpid());
}
  1. 说明
    输出进程id
  2. 问题
    暂无
  3. 命令 “./1-5.out”
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值