#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int open(const char *pathname, int flags);
int close(int fd);
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main() { char s[] = "abc.txt"; int fd = open(s, O_RDONLY); printf("fd=%d ", fd); return 0; }
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> #include <string.h>
int main()
{
char s[] = "abc.txt";
int fd = open(s, O_RDONLY);
if (fd == -1)
{
printf("error is %s ", strerror(errno));
}
else
{
printf("success fd=%d ", fd);
}
return 0;
}
ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, void *bug, size_t count);
int main() { char s[] = "abc.txt"; int fd = open(s, O_RDONLY); if (fd == -1) { printf("error is %s ", strerror(errno)); } else { printf("success fd=%d ", fd); char buf[100]; memset(buf, 0, sizeof(buf)); int i = read(fd, buf, sizeof(buf)); printf("%s ", buf); close(fd); } return 0; }
int main() { char s[] = "abc.txt"; int fd = open(s, O_RDONLY); if (fd == -1) { printf("error is %s ", strerror(errno)); } else { printf("success fd=%d ", fd); char buf[100]; memset(buf, 0, sizeof(buf)); while (read(fd, buf, sizeof(buf)) > 0) { printf("%s", buf); memset(buf, 0, sizeof(buf)); } close(fd); } return 0; }
int main() { char s[] = "abc.txt"; int fd = open(s, O_RDWR | O_APPEND); if (fd == -1) { printf("error is %s ", strerror(errno)); } else { printf("success fd=%d ", fd); char buf[100]; memset(buf, 0, sizeof(buf)); strcpy(buf, “hello world "); int i = write(fd, buf, strlen(buf)); close(fd); } return 0;
int fstat(int fd, struct stat *buf)
int stat(const char *path, struct stat *buf);
struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ dev_t st_rdev; /* device ID (if special file) */ off_t st_size; /* total size, in bytes */ blksize_t st_blksize; /* blocksize for filesystem I/O */ blkcnt_t st_blocks; /* number of blocks allocated */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last status change */ };
S_ISREG(m) is it a regular file?
S_ISDIR(m) directory?
S_ISCHR(m) character device?
S_ISBLK(m) block device?
S_ISFIFO(m) FIFO (named pipe)?
S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.)
S_ISSOCK(m) socket? (Not in POSIX.1-1996.)
int main() { char s[] = "abc.txt"; int fd = open(s, O_RDONLY); if (fd == -1) { printf("error is %s ", strerror(errno)); } else { printf("success "); struct stat buf; fstat(fd, &buf); if (S_ISREG(buf.st_mode)) { printf("%s is charfile ", s); } close(fd); } return 0; }
int main() { struct stat t; memset(&t, 0, sizeof(t)); if (stat("a.txt", &t) == 0) { printf("%d ", t.st_size); } else { printf("open failed %s ", strerror(errno)); } return 0; }
char *getpass( const char *prompt);
int main()
{
char *s = getpass("please input:");
printf("%s ", s);
return 0;
}