写操作
1 #include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
void error_handling(char* message); int main()
{
int fd;
char buf[] = "let's go!"; fd = open("1.txt",O_CREAT|O_WRONLY);
if(fd == -)
error_handling("open error");
printf("file descriptor:%d \n",fd); if(write(fd,buf,sizeof(buf)) == -)
error_handling("write error"); close(fd);
return ;
} void error_handling(char* message)
{
fputs(message,stderr);
fputc('\n',stderr);
exit();
}
写操作
1 #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
void error_handling(char* message); int main()
{
int fd;
char buf[];
fd = open("1.txt",O_RDONLY);
if(fd == -)
error_handling("open error");
printf("file description;%d\n",fd); if(read(fd,buf,sizeof(buf)-)==-)
error_handling("read error"); printf("file data:%s\n",buf);
close(fd);
return ;
} void error_handling(char* message)
{
fputs(message,stderr);
fputc('\n',stderr);
exit();
}