文章内容参考21天学通linux c编程
目录函数
mkdir rmdir
文件函数
creat remove
open close write read leek
文章内容参考21天学通linux c编程
目录函数
mkdir rmdir
新建目录
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
main()
{
extern int errno;
char *path="/root/tmp11";
if(mkdir(path,0766)==0)
{
printf("created the directory %s.\n",path);
}
else
{
printf("cant't creat the directory %s.\n",path);
printf("errno:%d\n",errno);
printf("ERR :%s\n",strerror(errno));
}
}
删除目录#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
main()
{
extern int errno;
char *path="/root/tmp11";
if(rmdir(path)==0)
{
printf("deleted the directory %s.\n",path);
}
else
{
printf("cant't delete the directory %s.\n",path);
printf("errno : %d\n",errno);
printf("ERR : %s\n",strerror(errno));
}
}
文件函数
creat remove
open close write read leek
---新建文件
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
main()
{
extern int errno;
char *path="/root/tmp.txt";
if(creat(path,0766)==-1)
{
printf("cant't create the file %s.\n",path);
printf("errno:%d\n",errno);
printf("ERR :%s\n",strerror(errno));
}
else
{
printf("created file %s.\n",path);
}
}
-----删除#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
main()
{
extern int errno;
char path[]="mytemp";
if(remove(path)==0)
{
printf("Deleted file %s.\n",path);
}
else
{
printf("cant't delete the file %s.\n",path);
printf("errno: %d\n",errno);
printf("ERR : %s\n",strerror(errno));
}
}
打开文件
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
main()
{
int fd,fd1;
char path[]="/root/txt1.txt";
extern int errno;
fd=open(path,O_WRONLY,0766);
if(fd!=-1)
{
printf("opened file %s .\n",path);
}
else
{
printf("cant't open file %s.\n",path);
printf("errno:%d\n",errno);
printf("ERR :%s\n",strerror(errno));
}
fd1=open(path,O_WRONLY|O_CREAT,0766);
if(fd1!=-1)
{
printf("opened file %s .\n",path);
}
else
{
printf("cant't open file %s.\n",path);
printf("errno:%d\n",errno);
printf("ERR :%s\n",strerror(errno));
}
}
关闭文件#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
main()
{
int fd;
char path[]="/root/txt1.txt";
extern int errno;
fd=open(path,O_RDONLY ,0766);
if(fd!=-1)
{
printf("opened file %s .\n",path);
}
else
{
printf("cant't open file %s.\n",path);
printf("errno:%d\n",errno);
printf("ERR :%s\n",strerror(errno));
}
if(close(fd)==0)
{
printf("closed.\n");
}
else
{
printf("close file %s error.\n",path);
printf("errno:%d\n",errno);
printf("ERR :%s\n",strerror(errno));
}
if(close(1156)==0)
{
printf("closed.\n");
}
else
{
printf("close file %s error.\n",path);
printf("errno:%d\n",errno);
printf("ERR :%s\n",strerror(errno));
}
}
写入
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
main()
{
int fd;
char path[]="/root/txt1.txt";
char s[]="hello ,Linux.\nI've leart C program for two weeks.\n";
extern int errno;
fd=open(path,O_WRONLY|O_CREAT);
if(fd!=-1)
{
printf("opened file %s .\n",path);
}
else
{
printf("cant't open file %s.\n",path);
printf("errno:%d\n",errno);
printf("ERR :%s\n",strerror(errno));
}
write(fd,s,sizeof(s));
close(fd);
printf("Done");
}
读文件
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
main()
{
int fd;
char path[]="/root/a.txt";
int size;
char s[100];
extern int errno;
fd=open(path,O_RDONLY);
if(fd!=-1)
{
printf("opened file %s .\n",path);
}
else
{
printf("cant't open file %s.\n",path);
printf("errno:%d\n",errno);
printf("ERR :%s\n",strerror(errno));
}
size=read(fd,s,sizeof(s));
close(fd);
printf("%s\n",s);
printf("%d\n",size);
close(fd);
}
读取重定位
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
main()
{
int fd;
char path[]="/root/txt1.txt";
int size;
char s[100]="";
extern int errno;
fd=open(path,O_RDONLY);
if(fd!=-1)
{
printf("opened file %s .\n",path);
}
else
{
printf("cant't open file %s.\n",path);
printf("errno:%d\n",errno);
printf("ERR :%s\n",strerror(errno));
}
size=read(fd,s,3);
printf("%d :",size);
printf("%s\n",s);
size=read(fd,s,3);
printf("%d :",size);
printf("%s\n",s);
lseek(fd,8,SEEK_SET);
size=read(fd,s,3);
printf("%d :",size);
printf("%s\n",s);
lseek(fd,0,SEEK_SET);
size=read(fd,s,3);
printf("%d :",size);
printf("%s\n",s);
close(fd);
}