文件I/O操作
标配的小号
本人CSDN博客已停止更新。更多更新的技术文章,请移步本人博客园,博客园链接为http://www.cnblogs.com/biaopei/
展开
-
readv与writev
[root@bogon mycode]# cat writev.c #include<stdio.h>#include<string.h>#include<unistd.h>#include<sys/uio.h>int main(){ char *str1="linux\n"; char *str2="windows\n"; struct iovec iov[2]原创 2017-05-25 14:58:59 · 392 阅读 · 0 评论 -
pread和pwrite函数
先来介绍pread函数[root@bogon mycode]# cat test.c#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<fcntl.h>char buf[20];void testpread(int fd1){ int i; printf("use pread\n");原创 2017-05-25 14:00:52 · 3447 阅读 · 0 评论 -
使用C语言简单模拟Linux的cat程序
先给出源码//fileio.c#include<stdio.h>#include<stdlib.h>#include<fcntl.h>void print(int fd){ int i,len; char buf[10]; len=read(fd,buf,10);//每read一次,文件偏移量就会偏移10个字节位置 while(len>0)//循环打印,直至原创 2017-05-25 10:23:49 · 1568 阅读 · 0 评论 -
将Linux文件清空的几种方法
1、使用重定向的方法[root@centos7 ~]# du -h test.txt 4.0K test.txt [root@centos7 ~]# > test.txt [root@centos7 ~]# du -h test.txt 0 test.txt2、使用true命令重定向清空文件[root@centos7 ~]# du -h test.txt 4.0K转载 2017-05-25 15:03:14 · 710 阅读 · 0 评论 -
fcntl获取和修改文件打开状态标志
[root@bogon code]# cat b.c#include<stdio.h>#include<error.h>#include<unistd.h>#include<fcntl.h>int main(){ int fd=open("a.c",O_RDONLY);//以可读方式打开 int flags; flags=fcntl(fd,F_GETFL);//用原创 2017-05-26 15:01:38 · 576 阅读 · 0 评论 -
输出内容时后面显示乱码
使用文件操作函数时,我遇到过几次,打印内容时内容没有错误,可是末尾多显示了几个乱码,其实主要是因为字符串末尾没有赋字符串结束符号\0[root@bogon mycode]# cat a.c#include<stdio.h>#include<fcntl.h>#include<unistd.h>#define MAX_SIZE 40int main(){ char buf[MAX_S原创 2017-05-25 23:03:05 · 776 阅读 · 0 评论 -
dup等复制文件描述符函数
[root@bogon code]# cat b.c#include<stdio.h>#include<error.h>#include<unistd.h>#include<fcntl.h>#define MAX_SIZE 5int main(){ int fd=open("a.c",O_RDWR); int fd1,fd2,fd3,len; char buf[原创 2017-05-26 15:40:28 · 341 阅读 · 0 评论 -
截断文件函数truncate和ftruncate
两个函数目的都是将文件大小设置为length参数指定的值 int truncate(const char *pathname,off_t length)//pathname就是路径 int ftruncate(int fd,off_t length);//该系统调用不会修改文件偏移量 其中truncate函数使用前不需要使用open函数打开文件,在系统调用中可谓独树一帜 ftruncate函原创 2017-05-26 16:17:00 · 3362 阅读 · 0 评论