- 目录的操作
DIR *dir;
struct dirent *entry;
struct stat stat1;
int fd;
int read_count=0;
int ret;
int main(int argc,char **argv)
{
dir = opendir(“./dir1/dir2”); // 打开目录
if (!dir)
{
printf(“can not open /dir1/dir2 \n”);
return -1;
}
while((entry = readdir(dir)))// 读目录的文件名
{
//
if(strcmp(entry->d_name,”1.txt”)==0)
{
printf(“find 1.txt \n”);
system(“ls ./dir1/dir2 -l “);
if ( (fd =open("./dir1/dir2/1.txt",O_RDWR))<0 )
{
printf("can't open \n");
}
if ((ret = fstat(fd, &stat1))==-1)
{
printf("can't get stat \n");
}
printf("size is %d\n",stat1.st_size);
}
}
return 0;
}
文件系统的读写
typedef struct file_storage {
int a;
int b;
int c;
char * buf1;
char * name;
int d;
}file_storage, *pt_file_storage;
int fd3=0;
int write_count=0;
file_storage t_file_storage;
file_storage t_file_storage_read;
int main(int argc,char **argv)
{
t_file_storage.a =10;
t_file_storage.b =20;
t_file_storage.c =30;
t_file_storage.buf1=malloc(sizeof("hello\n"));
t_file_storage.buf1="hello\n";
t_file_storage.name=malloc(sizeof("lxl\n"));
t_file_storage.name="lxl \n";
t_file_storage.d=1000;
fd3= open("storage.txt",O_RDWR|O_CREAT); // 不存在创建
write_count=write(fd3,&t_file_storage,sizeof(t_file_storage)+strlen("hello\n")+strlen("lxl\n"));
close(fd3);
fd3= open("storage.txt",O_RDWR|O_CREAT);
write_count= read(fd3,&t_file_storage_read,sizeof(t_file_storage)+strlen("hello\n")+strlen("lxl\n"));
close(fd3);
printf(“a is %d ,b is %d ,c is %d \n”,t_file_storage_read.a,t_file_storage_read.b,t_file_storage_read.c);
printf(“buf1 is %s name is %s\n”,t_file_storage_read.buf1,t_file_storage_read.name);
printf(“d is %d\n”,t_file_storage_read.d);
return 0;
}`