• 标准IO的简单应用,动静态库,读取系统时间并打印,模拟ls -l功能


    2015.2.27
    星期五,小雨

    标准IO实现的复制功能:

    #include <stdio.h>
    #include <errno.h>

    #define N 64

    int main(int argc, char *argv[])
    {
    int n;
    char buf[N];
    FILE *fps, *fpd;

    if(argc < 3)
    {
    printf("usage : %s <src_file><dst_file> ",argv[0]);
    return -1;
    }

    if((fps = fopen(argv[1],"r")) == NULL)
    {
    fprintf(stderr,"fail to fopen %s : %s ",argv[1],strerror(errno));
    return -1;
    }

    if((fpd = fopen(argv[2],"w")) == NULL)
    {
    fprintf(stderr,"fail to fopen %s : %s ",argv[2],strerror(errno));
    fclose(fps);
    return -1;
    }

    while((n = fread(buf, 1, N, fps)) > 0)
    {
    fwrite(buf, 1, n, fpd);
    }

    fclose(fps);
    fclose(fpd);
    return 0;

    }

    fgets()/fputs()/get()/putc()调用的代码:

    int main(void)
    {
    int c;
    while((c = getc(stdin)) != EOF)
    {
    if(putc(c, stdout) == EOF)
    {
    err_sys("output error");
    }
    }
    if(ferror(stdin))
    {
    err_sys("input error");
    }
    exit(0);
    }

    对比上下两个程序:

    int main(void)
    {
    char buf[MAXLINE];
    while((c = fgets(buf, MAXLINE, stdin)) != NULL)
    {
    if(fputs(buf, stdout) == EOF)
    {
    err_sys("output error");
    }
    }
    if(ferror(stdin))
    {
    err_sys("input error");
    }
    exit(0);
    }
    硬链接:ln 可删除,删除链接只会减少链接数,大小表示链接里面的内容的大小
    软链接:又称符号链接:ln -s ,删除链接的对象将不能访问之前的内容,大小表示链接对象符号自身的大小,不是里面内同的大小

    lg@lg-desktop:~/file/link$ ls -l
    total 0
    -rw-r--r-- 1 lg lg 0 2015-02-27 14:13 hello
    lg@lg-desktop:~/file/link$ echo 123 > hello
    lg@lg-desktop:~/file/link$ cat hello
    123
    lg@lg-desktop:~/file/link$ ls -l
    total 4
    -rw-r--r-- 1 lg lg 4 2015-02-27 14:14 hello
    lg@lg-desktop:~/file/link$ ln hello hardlink
    lg@lg-desktop:~/file/link$ ls -l
    total 8
    -rw-r--r-- 2 lg lg 4 2015-02-27 14:14 hardlink 链接数变成了2
    -rw-r--r-- 2 lg lg 4 2015-02-27 14:14 hello
    lg@lg-desktop:~/file/link$ ln -s hello slink
    lg@lg-desktop:~/file/link$ ls -l
    total 8
    -rw-r--r-- 2 lg lg 4 2015-02-27 14:14 hardlink
    -rw-r--r-- 2 lg lg 4 2015-02-27 14:14 hello
    lrwxrwxrwx 1 lg lg 5 2015-02-27 14:15 slink -> hello //这里的5表示slink的字符个数,软链接不增加链接数
    lg@lg-desktop:~/file/link$ cat hardlink //显示硬链接内容
    123
    lg@lg-desktop:~/file/link$ cat slink //显示软链接内容
    123
    lg@lg-desktop:~/file/link$ rm hello //删除原链接后再次查看软硬两种链接效果
    lg@lg-desktop:~/file/link$ cat hardlink //硬链接可显示,因为磁盘内容没删除,只有链接数变成0才会删除磁盘内容
    123
    lg@lg-desktop:~/file/link$ cat slink //软链接无法显示之前的内容,此时磁盘内容已删除
    cat: slink: No such file or directory
    lg@lg-desktop:~/file/link$

    静态库和动态库的不同点在于代码被载入的时刻不同:

    静态库在程序编译的时候会被链接到目标代码中,程序运行时不再需要该静态库,因此体积较大。
    动态库在程序编译时并不会链接到目标代码中,而是在程序运行的时候才被载入,因此程序运行时还需要动态库的存在,代码体积较小。

    静态库:
    gcc -c xx.c 生成多个xx.o文件
    ar -crsv libpr.a pr1.o pr2.o 静态库打包完成
    gcc main.c -lpr -L. (最后的-L.是表示在当前路径下查找库) 链接刚才创建的静态库,别人没有源文件,只有编译好的库

    共享库的生成:
    main.c Makefile pr1.c pr2.c
    gcc -c pr1.c pr2.c -fPIC(创建于位置无关的编译程序)
    gcc -o libpr.so -shared pr1.o pr2.o //共享库创建完成
    gcc main.c -lpr -L.

    没隔1秒读取一次系统时间并写入文件:

    #include <stdio.h>
    #include <time.h>
    #include <unistd.h>

    #define N 64

    int main(int argc, char *argv[])
    {
    FILE *fp;
    time_t t;

    if(argc < 2)
    {
    printf(" usage : %s <file> ",argv[0]);
    return -1;
    }

    if((fp = fopen(argv[1],"w")) == NULL)
    {
    perror("fail to open");
    return -1;
    }

    while(1)
    {
    time(&t);
    fprintf(fp,"%s ",ctime(&t));
    //fwrite(ctime(&t),1,24,fp); //用fwrite实现相同的效果
    //fprintf(fp," ");
    fflush(fp); //需要加强制刷新
    sleep(1);
    }

    fclose(fp);

    return 0;
    }

    程序运行效果:
    lg@lg-desktop:/mnt/hgfs/source test/file IO$ gcc mytime.c
    lg@lg-desktop:/mnt/hgfs/source test/file IO$ ./a.out time
    ^C
    lg@lg-desktop:/mnt/hgfs/source test/file IO$ cat time
    Fri Feb 27 20:09:36 2015

    Fri Feb 27 20:09:37 2015

    Fri Feb 27 20:09:38 2015

    lg@lg-desktop:/mnt/hgfs/source test/file IO$

    模拟ls -l的功能:


    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    #include <pwd.h>
    #include <grp.h>
    #include <time.h>

    #define MAXLEN_SYMBLINK 64
    #define TEST_TYPE 2

    int main(int argc, char *argv[])
    {
    struct stat buf;
    char out[MAXLEN_SYMBLINK + 1];
    struct tm *t;
    ssize_t n;

    if(argc != 2)
    {
    fprintf(stderr,"Usage: %s <pathname> ",argv[0]);
    return 0;
    }

    if(lstat(argv[1],&buf) < 0)
    {
    perror("lstat error");
    return -1;
    }

    switch(buf.st_mode & S_IFMT)
    {
    case S_IFREG:
    default: printf("-");break;
    case S_IFDIR: printf("d"); break;
    case S_IFCHR: printf("c"); break;
    case S_IFBLK: printf("b"); break;
    case S_IFIFO: printf("p"); break;
    case S_IFLNK: printf("l"); break;
    case S_IFSOCK: printf("s"); break;

    }

    if(S_IRUSR & buf.st_mode) printf("r");
    else printf("-");

    if(S_IWUSR & buf.st_mode) printf("w");
    else printf("-");

    if(S_IXUSR & buf.st_mode) printf("x");
    else printf("-");

    if(S_IRGRP & buf.st_mode) printf("r");
    else printf("-");

    if(S_IWGRP & buf.st_mode) printf("w");
    else printf("-");

    if(S_IXGRP & buf.st_mode) printf("x");
    else printf("-");

    if(S_IROTH & buf.st_mode) printf("r");
    else printf("-");

    if(S_IWOTH & buf.st_mode) printf("w");
    else printf("-");

    if(S_IXOTH & buf.st_mode) printf("x");
    else printf("-");

    printf(" %d",buf.st_nlink);

    struct passwd *pw;
    pw = getpwuid(buf.st_uid);
    printf(" %s",pw->pw_name);

    struct group *gr;
    gr = getgrgid(buf.st_gid);
    printf(" %s",gr->gr_name);

    printf(" %4ld",buf.st_size);

    t = localtime(&buf.st_mtime);
    printf(" %04d-%02d-%02d %02d:%02d",
    t->tm_year + 1900,
    t->tm_mon + 1,
    t->tm_mday,
    t->tm_hour,
    t->tm_min);

    printf(" %s ",argv[1]);

    if(S_ISLNK(buf.st_mode))
    {
    printf(" -> ");
    if(-1 == (n = readlink(argv[1], out, MAXLEN_SYMBLINK)))
    {
    perror("readlink error");
    }
    else
    {
    out[n] = 0x00;
    printf("%s",out);
    }
    }

    printf(" ");

    return 0;
    }

    程序运行效果:
    lg@lg-desktop:/mnt/hgfs/source test/file IO$ ls -l time
    -rwxrwxrwx 1 root root 78 2015-02-27 20:09 time
    lg@lg-desktop:/mnt/hgfs/source test/file IO$ ./a.out time
    -rwxrwxrwx 1 root root 78 2015-02-27 20:09 time
    lg@lg-desktop:/mnt/hgfs/source test/file IO$

    ***************************************************************************************************************************************************************
    ***************************************************************************************************************************************************************
    ***************************************************************************************************************************************************************
    ***************************************************************************************************************************************************************

  • 相关阅读:
    Alpha阶段项目复审
    复审与事后分析
    测试与发布(Alpha版本)
    第七天
    第六天
    团队作业第4周——项目冲刺
    第一天
    第二天
    第四天
    第五天
  • 原文地址:https://www.cnblogs.com/cnlg/p/4304399.html
Copyright © 2020-2023  润新知