opendir.c
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
int main()
{
DIR *dir;
struct dirent *ptr;
struct stat filestat;
dir = opendir("file");
chdir("file");
while((ptr = readdir(dir)) != NULL )
{
if(strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0)
continue;
stat(ptr->d_name,&filestat);
printf("file name :%s\t size :%d\n",ptr->d_name,filestat.st_size);
}
return 0;
}
stat.c(文件状态)
#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h>
int main()
{
struct stat buf;
stat("stat.c",&buf);
printf("size = %d\n",buf.st_size);
return 0;
}
filefd.c(open,lseek等就用)
#include<stdio.h>
#include<sys/types.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
char buf[] = "This is test file";
int fd ;
fd = open("testfile",O_CREAT|O_RDWR);
perror("open");
write(fd,buf,sizeof(buf));
perror("write");
lseek(fd,100,SEEK_SET);
perror("lseek");
close(fd);
sleep(1);
printf("\n");
fd = open("testfile",O_RDWR);
perror("open");
char buf_1[128];
read(fd,buf_1,128);
printf("buf_1 = %s\n",buf);
close(fd);
return 0;
}