mirror of
https://github.com/riba2534/TCP-IP-NetworkNote.git
synced 2026-02-03 01:53:19 +08:00
29 lines
577 B
C
29 lines
577 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#define BUF_SIZE 100
|
|
void error_handling(char *message);
|
|
|
|
int main()
|
|
{
|
|
int fd;
|
|
char buf[BUF_SIZE];
|
|
|
|
fd = open("data.txt", O_RDONLY);
|
|
if (fd == -1)
|
|
error_handling("open() error!");
|
|
printf("file descriptor: %d \n", fd);
|
|
|
|
if (read(fd, buf, sizeof(buf)) == -1)
|
|
error_handling("read() error!");
|
|
printf("file data: %s", buf);
|
|
close(fd);
|
|
return 0;
|
|
}
|
|
void error_handling(char *message)
|
|
{
|
|
fputs(message, stderr);
|
|
fputc('\n', stderr);
|
|
exit(1);
|
|
} |