Unix/Linux编程实践教程(0:)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unix/Linux编程实践教程(0:)相关的知识,希望对你有一定的参考价值。

本来只打算读这本书socket等相关内容,但书写得实在好,还是决定把其余的内容都读一下。

 

阅读联机帮助的一个示例:

技术分享

 

open系统调用:

技术分享

read系统调用:

技术分享

Unix的time:

技术分享

技术分享

技术分享

上面的printf可以看到,一个临时的char* 指针也可以+4,希望查看ctime函数里面是否有malloc,如果有的话由谁来释放内存???没有的话为什么可以指针操作。

为解决上述疑惑,通过查看http://www.cplusplus.com/reference/ctime/ctime/以及及http://www.cplusplus.com/reference/ctime/asctime/,得到:

ctime
char* ctime (const time_t * timer);
Convert time_t value to string

This function is equivalent to: 
 
asctime(localtime(timer))


asctime
char* asctime (const struct tm * timeptr);
Convert tm structure to string

It is defined with a behavior equivalent to:

char* asctime(const struct tm *timeptr)
{
  static const char wday_name[][4] = {
    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  };
  static const char mon_name[][4] = {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  };
  static char result[26];
  sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
    wday_name[timeptr->tm_wday],
    mon_name[timeptr->tm_mon],
    timeptr->tm_mday, timeptr->tm_hour,
    timeptr->tm_min, timeptr->tm_sec,
    1900 + timeptr->tm_year);
  return result;
}

疑惑解除了,可以看到ctime返回的是一个静态局部char数组。

 

系统调用lseek改变打开文件位置:

技术分享

 

处理系统调用中的错误

确定错误种类errno。

显示错误信息perror:

技术分享

ls -a选项列出隐藏文件:

技术分享

如何编写ls:

技术分享

stat获取文件属性信息:

技术分享

其中stat的结构为:

技术分享

 

将模式字段转换为字符

技术分享

使用掩码得到文件类型:

技术分享

 

以上是关于Unix/Linux编程实践教程(0:)的主要内容,如果未能解决你的问题,请参考以下文章

Unix/Linux编程实践教程

软件开发知识体系

Unix, Linux 和MacOS

Unix/Linux环境C编程新手教程(12) openSUSECCPP以及Linux内核驱动开发环境搭建

Unix/Linux环境C编程新手教程(22) C/C++怎样获取程序的执行时间

Unix教程_编程入门自学教程_菜鸟教程-免费教程分享