//read函数
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /*
STDIN_FILENO:标准输入,值是0
STDOUT_FILENO:标准输出,值是1
STDERR_FILENO:标准错误,值是2
头文件是 unistd.h read(文件标识符,缓冲区,缓冲区大小)函数是从某个文件中读取缓冲区大小的数据
ssize_t read(int fd, void *buf, size_t count);
read()函数的返回值是输入字符的个数+1(+1是因为系统会为输入的字符串加上'\0')
read()相比于scanf()优势在于read()可以读取空格,而scanf()遇到空格结束
但是read不能将数字字符串直接转化成int型,需要调用atoi()函数
read()函数可以读取键盘上输入的每个字符包括回车换行 atoi()函数的头文件是stdlib.h,功能是将字符串转成数字
*/ int main(int arg,char *args[])
{
char buf[]={};
int numx=;
int index=read(STDIN_FILENO,buf,sizeof(buf));
printf("返回值是%d,输入字符串是%s\n",index,buf);
numx=atoi(buf);
printf("输入的数字是%d\n",numx);
return ;
}
//write函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> /*
ssize_t write(int fd, const void *buf, size_t count);
write()函数的返回值是写入字符串的大小(不包含'\0')
write()函数向指定文件标识符输出缓冲区数据
*/ int main(int arg,char *args[])
{
char buf[]={};
strcpy(buf,"fly with you!\n");
int index=write(STDOUT_FILENO,buf,strlen(buf));
printf("返回值是%d,\n",index);
return ;
}
//open函数|close()函数
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h> /*
int open(const char *pathname, int flags);
Opne试图打开参数pathname中的一个文件。
参数flags指定访问该文件的方式。
必须把flags设置为O_RDONLY,O_WRONLY,O_RDWR,O_CREAT,O_APPEND分别表示只读、只写、读写、如果文件存在就创建、追加。
open成功后会返回一个文件描述符。
open失败后会返回-1,并设置errno变量 errno是int型,表示错误编号,头文件是errno.h
strerror()函数是打印错误信息,头文件是string.h
*/ int main(int arg,char *args[])
{
if(arg<)
{
printf("请输入一个参数!\n");
return ;
}
int fd=open(args[],O_RDONLY);
if(fd==-)
{
printf("错误信息:%s\n",strerror(errno));
}else
{
printf("文件标识符是%d\n",fd);
char buf[]={};
//读取当前文件的内容
read(fd,buf,sizeof(buf)-);
printf("%s\n",buf);
close(fd);
}
return ;
}
使用fstat函数获取文件信息。
int fstat(int fd,struct stat *buf)
参数fd必须使用open()函数调用返回的有效文件描述符
使用stat函数获取文件信息
int stat(const char *path,struct stat *buf)
参数path是路径加文件名的字符串 结构体stat
struct stat
{
dev_t st_dev; /*ID of device containing file*/
ino_t st_ino; /*inode number*/
mode_t st_mode; /*protection*/
nlink_t st_nlink; /*numbers of hard links*/
uid_t st_uid; /*user ID of owner*/
gid_t st_gid; /*group ID of owner*/
dev_t st_rdev; /*device ID (if special file)*/
off_t st_size; /*total size,in bytes*/
blksize_t st_blksize; /*blocksize for filesystem I/O*/
blkcnt_t st_blocks; /*number of blocks allocated*/
time_t st_atime; /*time of last access*/
time_t st_mtime; /*time of last modification*/
time_t st_ctime; /*time of last status change*/
}; 为了正确解释文件类型,有一套宏能够计算stat借口的st_mode成员。
S_ISREG(m) is it a regular file?
S_ISDIR(m) directory?
S_ISCHR(m) character device?
S_ISBLK(m) block device?
S_ISFIFO(m) FIFO (named pipe)?
S_ISLNK(m) symbolic lin?(Not in POSIX.-)
S_ISSOCK(m) socket?(Not in POSIX.-)
//fstat()函数
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h> int main(int arg,char *args[])
{
if(arg<)
{
printf("请输入一个参数!\n");
return ;
}
//open file
/*
备注:如果是以追加的方式打开文件,必须使用 O_RDWR|O_APPEND;单独的是O_APPEND没有效果!
*/
int fd=open(args[],O_RDONLY);
//Judgment file identifier
if(fd==-)
{
printf("error msg:%s\n",strerror(errno));
}else
{
printf("file identifier:%d\n",fd);
//define struct stat
struct stat buf;
//get file stat
fstat(fd,&buf);
//judge file tyep
if(S_ISREG(buf.st_mode))
{
printf("this is regular file!\n");
}else if(S_ISDIR(buf.st_mode))
{
printf("this id directory!\n");
}
//show file size
printf("file size:%d\n",buf.st_size);
//close file
close(fd);
}
return ;
}
getpass()函数
读写用户输入,屏幕不回显(一般用于输入密码不显示)
char * getpass(const char * prompt);
getpass用于从键盘读取用户输入,但屏幕不回显。
参数prompt为屏幕提示字符。
函数返回值为用户键盘输入字符串
//getpass()函数的使用
#include <stdio.h>
#include <unistd.h> int main(int arg,char *args[])
{
char *s=getpass("input password:");
printf("your password :%s\n",s);
return ;
}

Linux 文件管理(系统函数)的更多相关文章

  1. Linux常用系统函数

    Linux常用系统函数 一.进程控制 fork 创建一个新进程clone 按指定条件创建子进程execve 运行可执行文件exit 中止进程_exit 立即中止当前进程getdtablesize 进程 ...

  2. 标准c库函数与Linux下系统函数库 区别 (即带不带缓冲区的学习)

    我们都知道,C语言在UNIX/Linux系统下有一套系统调用(系统函数),比如文件操作open().close().write().read()等,而标准C语言的库函数中也有一套对文件的操作函数fop ...

  3. linux文件管理 -> 系统文件属性

    -rw-------. 1 root root 4434 May 30 13:58 ks.cfg -rw-------. ①:文件类型与权限 ②:硬链接次数 root ③:所属用户 root ④:所属 ...

  4. linux文件管理 -> 系统压缩打包

    如果希望windows和Linux互相能使用的压缩工具, 建议.zip格式 压缩的好处主要有: 节省磁盘空间占用率 节省网络传输带宽消耗 网络传输更加快捷 Linux系统常见的后缀名所对应的压缩工具 ...

  5. 【Linux程序设计】之环境系统函数综合实验

    这个系列的博客贴的都是我大二的时候学习Linux系统高级编程时的一些实验程序,都挺简单的.贴出来纯粹是聊胜于无. 实验题目:Linux环境下系统函数综合实验 实验目的:熟悉并掌握Linux环境下数学函 ...

  6. Linux下系统时间函数、DST等相关问题总结(转)

    Linux下系统时间函数.DST等相关问题总结 下面这个结构体存储了跟时区相关的位移量(offset)以及是否存在DST等信息,根据所在的时区信息,很容易找到系统时间与UTC时间之间的时区偏移,另外根 ...

  7. linux 系统函数之 (dirname, basename)【转】

    转自:http://blog.csdn.net/peter_cloud/article/details/9308333 版权声明:本文为博主原创文章,未经博主允许不得转载. 除非你的原件考虑跨平台. ...

  8. linux 系统函数 basename和dirname

    在linux系统中有这样两个系统函数,basename 和  dirname 1.basename 用于 获取文件名, 1.1 当给定扩展名作为参数之后,甚至可以直接获取文件名 2.与basename ...

  9. Linux网络编程2——系统函数

    socket信息数据结构 #include <netinet/in.h> struct sockaddr { unsigned short sa_family; /*地址族*/ ]; /* ...

  10. 标准c库函数和linux系统函数的关系

    c库IO函数的工作流程 c库函数与系统函数的关系 虚拟地址空间 文件描述符

随机推荐

  1. 用Qemu模拟vexpress-a9 (二) --- 搭建u-boot调试环境

    参考: http://blog.csdn.net/caspiansea/article/details/12986565 环境介绍 Win7 64 + Vmware 11 + ubuntu14.04 ...

  2. js时间小总结

    1.js获取时间 var myDate = new Date(); 1 myDate.getYear(); //获取当前年份(2位) 2 myDate.getFullYear(); //获取完整的年份 ...

  3. tengine + lua 实现流量拷贝

    环境搭建参考地址:http://www.cnblogs.com/cp-miao/p/7505910.html cp.lua local res1, res2, action action = ngx. ...

  4. 创建 git仓库

    首先创建一个文件夹作为git仓库,创建一个test文件夹,并在文件夹下创建一个test.c的文件用以测试: git init git使用git init来初始化一个git仓库,git的很多命令都是在g ...

  5. 计算两个经纬度之间的距离(python算法)

    EARTH_REDIUS = 6378.137 def rad(d): return d * pi / 180.0 def getDistance(lat1, lng1, lat2, lng2): r ...

  6. mongodb 踩坑记录

    Map-Reduce Map-Reduce 是 mongodb 处理批量数据的大杀器,凡是数据量大并且定时处理能满足需求的,都可以试着扔给 mongodb,让它去 Map-Reduce. 以下截取自文 ...

  7. HTML5 Canvas 绘制二十四字真言钟表

    代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...

  8. 使用Python实现生产者消费者问题

    之前用C++写过一篇生产者消费者的实现. 生产者和消费者主要是处理互斥和同步的问题: 队列作为缓冲区,需要互斥操作 队列中没有产品,消费者需要等待,直到生产者放入产品并通知它.队列慢的情况类似. 这里 ...

  9. CentOS 6.4 图文安装教程(有些设置大部分教程没出现过)

    http://www.jb51.net/os/78318.html CentOS 6.4 下载地址: http://www.jb51.net/softs/78243.html 1.首先,要有一张Cen ...

  10. scrollTop()--返回或设置匹配元素的滚动条的垂直位置

    scrollTop() 方法返回或设置匹配元素的滚动条的垂直位置. scroll top offset 指的是滚动条相对于其顶部的偏移. 如果该方法未设置参数,则返回以像素计的相对滚动条顶部的偏移. ...