全部博文(489)
分类:
2011-12-22 20:54:39
原文地址:C 文件系统编程 作者:luozhiyong131
#include
#include
#include
#include
#include
#include
#include
#include
#define Name 16
#define Num 32
struct student
{
unsigned int id;
char name[Name];
};
int main(int argc, char **argv)
{
int fd,id;
struct student std;
char name[Name];
if(argc!=2)
{
printf("too few arguments\n");
exit(1);
}
fd = open(argv[1],O_WRONLY|O_CREAT,S_IRUSR|S_SIWUSR);
if(fd==-1)
{
perror("open faild");
exit(1);
}
printf("Please input the student informayion\n");
for(;;)
{
printf("ID: ");
scanf("%d",&id);
if(id<0)
{
printf(Complete!\n);
exit(0);
}
else
{
printf("Name:");
scanf("%16s", name);
std.id=id;
bzero(std.name, Name);
strcpy(std.name, name);
lseek(fd, id*sizeof(std), SEEK_SET);
write(fd, (char *)&std, sizeof(std));
}
}
close(fd);
return 0;
}
#include
#include
#include
#include
int main(int argc, char **argv)
{
struct stat st;
if(argc!=2)
{
printf("too few arguments\n");
exit(1);
}
stat(argv[1],&st);
printf("File Size:%dld Byrte\n",st.st_size);
printf("File Type:")
switch(st.st_mode&S_IFMT)
{
case S_IFSOCK;
printf("Socket\n");
break;
case S_IFLNK;
printf("Symbolic link\n");
break;
case S_IFREG;
printf("Regular file\n");
break;
case S_IFBLK;
printf("Block device\n");
break;
case S_IFDIR;
printf("Directory\n");
break;
case S_IFCHR;
printf("Character device\n");
break;
case S_IFIFO;
printf("FIFO\n");
break;
default;
}
return 0;
}
/**************
创建目录文件
***************/
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char **argv)
{
int ret;
if(argc!=2)
{
printf("too few arguments\n");
exit(1);
}
ret = mkdir(argv[1],S_IRUSR|S_IXUSR);
if(ret==-1)
{
perror("mkdir failed\n");
exit(1);
}
printf("Done\n")
return 0;
}
/*******************************
*列出指定目录下所有文件和子目录*
********************************/
#include
#include
#include
#include
#include
#include
int main(int argc, char **argv)
{
DIR *dp;
struct dirent *dirp;
if(argc!=2)
{
printf("too few arguments\n");
exit(1);
}
dp = opendir(argv[1]);
if(dp==NULL)
{
perror("opendir error");
exit(1);
}
while((dirp = readdir(dp))!=NULL)
{
if((strcmp(dirp->d_name,".")==0)||(strcmp(dirp->d_name,"..")==0))
continue;
if(dirp->d_type==DT_DIR)
{
printf("%s
}
else
printf("%s\n",dirp->d_name);
}
closedir(dp);
return 0;
}
/*********
*工作目录*
**********/
#include
#include
#include
#include
#include size 64
int main()
{
int ret;
char buf[size];
getcwd(buf,sizeof(buf));
printf("%s\n",buf);
ret = chdir("..");
if(ret == -1)
{
perror("chdir error");
exit(1);
}
getcwd(buf,sizeof(buf));
printf("%s\n",buf);
return 0;
}
/***********************
*获取指定文件的访问权限*
************************/
#include
#include
#include
int main(int argc, char **argv)
{
if(argc!=2)
{
printf("too few arguments\n");
exit(1);
}
if(access(argv[1],F_OK)!=-1)
{
if(access(argv[1],R_OK)!=-1)
printf("read\n");
if(access(argv[1],W_OK)!=-1)
printf("write\n");
if(access(argv[1],X_OK)!=-1)
printf("execute\n");
}
else
{
printf("%s dose not exist\n",argc[1]);
}
return 0;
}
/*******************
*修改文件的访问权限*
********************/
#include
#include
#include
#include
#include
int main(int argc, char **argv)
{
int ret;
if(argc!=2)
{
printf("too few arguments\n");
exit(1);
}
ret = chmod(argv[1],S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if(ret==-1)
{
perror("chmod failed");
exit(1);
}
printf("Done\n");
return 0;
}