Unix/Linux?е?read??write????
???????????? ???????[ 2016/12/19 10:22:39 ] ????????Linux ??????
????1???????????
??????????????????д???????????????????????á????????????????????????????????????????????????????????????????????????????????????д????????????open??create???????????????????????????????????????read??write??????
????2??write????
????write???????????£?
????#include < unistd >
????ssize_t write(int filedes?? void *buf?? size_t nbytes);
????// ?????????????д???????????????????-1
????// filedes???????????
????// buf:??д???????????
????// nbytes:?д????????
????
write?????????????
#include <error.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
int main()
{
int fd = open("./write.txt"?? O_WRONLY | O_CREAT?? 0666);
if (-1 == fd){
perror("open file error");
return;
}
char* buff = "write data to file!";
ssize_t writelen = write(fd?? buff?? strlen(buff));
printf("%d
"?? writelen);
close(fd);
return 0;
}
????3??read????
????read???????????£?
????#include < unistd >
????ssize_t read(int filedes?? void *buf?? size_t nbytes);
????// ??????????????????????????????????β????0???????????-1
????// filedes???????????
????// buf:????????????
????// nbytes:???????????
?????м???????????????????????????????????????
????1?????????????????????????????????????????????磬???????????????????30????????????100????????read????30????????????read???????????0???????????
????2??????????豸??????????ζ????С?
????3???????????????????е??????????????????С?????????????????
????4??????????FIFO??????????????????????????????????????read?????????????????????
????5???????Щ?????????豸?????????????????ζ???????????
????6???????????????ж????????????????????
????
read?????????????
#include <error.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
int main()
{
int fd = open("./read.txt"?? O_RDONLY | O_CREAT?? 0666);
if (-1 == fd){
perror("open file error");
return;
}
char buff[1024];
memset(buff?? 0?? sizeof(buff));
ssize_t readlen = read(fd?? buff?? 1024);
printf("%s"?? buff);
printf("%d
"?? readlen);
close(fd);
return 0;
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11