Linux的目录操作、结构体的文件系统的读写

  1. 目录的操作
    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;
}`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值