运行结果:(由于write写入文件的内容是二进制python s ixusr,所以能够直接查看,查看方法参看第五部分)
$ ./a b
write 40 bytes to b
write 40 bytes to b
write 40 bytes to b
$
/*read*/
*************************************************************************
> 头文件 -> #include <unistd.h>
> ssize_t read(int fd, void *buf, size_t count);
> ssize_t read(文件描述符,写入数据的地址,写入数据的长度);
> 参数一 -> fd -> 文件描述符
> 参数二 -> buf -> 读取数据存放的地址
> 参数三 -> count -> 获得数据的长度
> 返回值(成功) -> 实际读到的字节数
> 返回值(失败) -> 出错返回-1并设置errno
> 作用 -> 读取文件
*************************************************************************
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char *argv[]){
char caBuf[1024] = {'\0'};
read(STDIN_FILENO, caBuf, sizeof(caBuf));
// 在caBuf中查找是否包含字符'\n'
// 若存在则返回其地址
// 若不存在则返回NULL
char *p = strchr(caBuf, '\n');
if (NULL != p){
*p = '\0';
}
printf("----------------------\n");
printf("%s\n", caBuf);
return 0;
}

运行结果:
$ ./a
Hello World!
----------------------
Hello World!
$
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char *argv[]){
char caBuf[1024] = {'\0'};
// scanf("%s", caBuf);
// gets(caBuf);
// 从标准输入读取数据放入caBuf中
printf("%d\n", STDIN_FILENO); // 0
// 若输入的数据量小于要求读取的数据量
// 则会将换行当做正常的字符读取
read(STDIN_FILENO, caBuf, sizeof(caBuf));
printf("%s\n", caBuf);
return 0;
}
运行结果:
$ ./a
0
Hello World!
Hello World!
$
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char *argv[]){
// fdsrc -> 需要被复制的源文件
int fdsrc = open(argv[1], O_RDONLY);
if (-1 == fdsrc){
perror("open src");
return -1;
}
// fddest -> 目标文件
int fddest = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0664);
if (-1 == fddest){
perror("open dest");
return -1;
}
char caBuf[4096] = {'\0'};
ssize_t ret = -1;
while (1){
memset(caBuf, '\0', sizeof(caBuf));
// read -> fdsrc
ret = read(fdsrc, caBuf, sizeof(caBuf));
if (0 == ret || -1 == ret){
break;
}
// write -> fddest
write(fddest, caBuf, ret);
}
close(fdsrc);
close(fddest);
return 0;
}
运行结果:
$ cat c
$ cat b
Hello World!
$ ./a
open src: Bad address
$ ./a b
open dest: Bad address
$ ./a b c
$ cat c
Hello World!
$
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char *argv[]){
int fd = -1;
fd = open(argv[1], O_RDONLY);
if (-1 == fd){
perror("open file");
}else{
printf("open %s ok, fd = %d\n", argv[1], fd);
char caBuf[1024] = {'\0'};
ssize_t ret = -1;
while (1){
ret = read(fd, caBuf, sizeof(caBuf));
if (-1 == ret){
perror("read");
break;
}else if(0 == ret){
break;
}
printf("read %d bytes from %s\n", ret, argv[1]);
printf("data:%s\n",caBuf);
}
// 关闭文件
close(fd);
}
return 0;
}
运行结果:
$ ./a
open file: Bad address
$ ./a b
open b ok, fd = 3
read 13 bytes from b
data:Hello World!
$
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
typedef struct STU{
int iId;
char caName[32];
float fScore;
}STU;
int main(int argc, char *argv[]){
int fd = -1;
fd = open(argv[1], O_RDONLY);
if (-1 == fd){
perror("open file");
}else{
STU stu;
ssize_t ret = -1;
while (1){
ret = read(fd, &stu, sizeof(STU));
if (0 == ret){
printf("到达了文件末尾,结束\n");
break;
}
if (-1 == ret){
perror("read");
break;
}
printf("id=%d,name=%s,score=%.2f\n"
, stu.iId, stu.caName, stu.fScore);
}
// 关闭文件
close(fd);
}
return 0;
}
运行结果:(由于read读取文件的方法是二进制,需要结合本笔记第四部分的内容配套查看)
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shumachanpin/article-121793-2.html
这也是一种方法
这是美国娇嫩的军舰所不喜欢的