在linux中,任何设备,文件,目录...都是以文件的形式存放的。
1.比较重要的设备文件:
/dev/console 系统控制台,出错和诊断信息通常会被发送到这里
/dev/tty 进程控制终端(键盘和显示器)的一个假名,如ls -R | more,需要键盘操作才能显示下一页
/dev/null 空设备,所有写向这个设备的输出都将被抛弃。
如: $ echo do not want to see this >/dev/null
$ cp /dev/null empty_file
2.系统底层访问
#include <unistd.h>
#include <stdlib.h>
write系统调用:
size_t write(int fildes, const void *buf, size_t nbytes);
fildes:文件描述符
*buf: 缓冲区
nbytes: 字节数
把缓冲区buf里的前nbytes个字节写入与文件描述符filds相关的文件中去。
返回实际写入的字节数,返回0表示没写入,返回-1表示出错
read系统调用:
size_t read(int fildes, const void *buf, size_t nbytess);
从文件描述符fildes相关的文件里读入nbytes个字节的数据并把它放在数据区buf中去。
其他与write同。
open系统调用:
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int open(const char *path, int oflags);
int open(const char *path, int oflags, mode_t mode);
open建立一条到文件或设备的访问路径,返回一个文件描述符,供write和read调用。
oflags: O_APPEND, O_TRUNC, O_CREAT, O_EXCL
mode中标志位按OR操作后得到的:
S_IRUSR,S_IWUSR, S_IXUSR...(USR,GRP,OTH);
如: open("myfile", O_CREAT, S_IRUSR | S_IXOTH);
$ls -ls myfile
0 -r------x 1 song software 0 sep 21 06:34 myfile*
umask变量:对文件的访问权限设定一个掩码。
close系统调用:
int close (int fildes);
ioctl系统调用:
int ioctl(int fildes,int cmd,...);
提供对设备访问等方面进行控制的操作界面。
lseek, fstat, stat, lstat, dup和dup2系统调用;
例子:
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main()
{
char c;
int in, out;
in=open("file.in",O_RDONLY);
out=open("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
while(read(in,&c,1)==1)
write(out,&c,1);
exit (0);
}
3.标准I/O库
#include <stdio.h>
FILE *fopen(const char *filename, const char *mode);
size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
size_t fwrite(const void *ptr, sizet size, size_t nitems, FILE *stream);
int fclose(FILE *stream);
int fflush(FILE *stream);
int fseek(FILE *stream, ong int offset, int whence);
.....
int printf(const char *format, ....);
int scanf(const char *format, ...);
int ferror(FILE *stream);
int feof(FILE *stream);
void clearerr(FILE *stream);
4.文件和子目录的维护
#include <sys/stat.h>
int chmod(const char *path, mode_t mode);
int chown(const char *path, uid_t owner, gid_t group);
mkdir,rmdir,opendir,closedir,readdir,telldir,seekdir...
1.比较重要的设备文件:
/dev/console 系统控制台,出错和诊断信息通常会被发送到这里
/dev/tty 进程控制终端(键盘和显示器)的一个假名,如ls -R | more,需要键盘操作才能显示下一页
/dev/null 空设备,所有写向这个设备的输出都将被抛弃。
如: $ echo do not want to see this >/dev/null
$ cp /dev/null empty_file
2.系统底层访问
#include <unistd.h>
#include <stdlib.h>
write系统调用:
size_t write(int fildes, const void *buf, size_t nbytes);
fildes:文件描述符
*buf: 缓冲区
nbytes: 字节数
把缓冲区buf里的前nbytes个字节写入与文件描述符filds相关的文件中去。
返回实际写入的字节数,返回0表示没写入,返回-1表示出错
read系统调用:
size_t read(int fildes, const void *buf, size_t nbytess);
从文件描述符fildes相关的文件里读入nbytes个字节的数据并把它放在数据区buf中去。
其他与write同。
open系统调用:
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int open(const char *path, int oflags);
int open(const char *path, int oflags, mode_t mode);
open建立一条到文件或设备的访问路径,返回一个文件描述符,供write和read调用。
oflags: O_APPEND, O_TRUNC, O_CREAT, O_EXCL
mode中标志位按OR操作后得到的:
S_IRUSR,S_IWUSR, S_IXUSR...(USR,GRP,OTH);
如: open("myfile", O_CREAT, S_IRUSR | S_IXOTH);
$ls -ls myfile
0 -r------x 1 song software 0 sep 21 06:34 myfile*
umask变量:对文件的访问权限设定一个掩码。
close系统调用:
int close (int fildes);
ioctl系统调用:
int ioctl(int fildes,int cmd,...);
提供对设备访问等方面进行控制的操作界面。
lseek, fstat, stat, lstat, dup和dup2系统调用;
例子:
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main()
{
char c;
int in, out;
in=open("file.in",O_RDONLY);
out=open("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
while(read(in,&c,1)==1)
write(out,&c,1);
exit (0);
}
3.标准I/O库
#include <stdio.h>
FILE *fopen(const char *filename, const char *mode);
size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
size_t fwrite(const void *ptr, sizet size, size_t nitems, FILE *stream);
int fclose(FILE *stream);
int fflush(FILE *stream);
int fseek(FILE *stream, ong int offset, int whence);
.....
int printf(const char *format, ....);
int scanf(const char *format, ...);
int ferror(FILE *stream);
int feof(FILE *stream);
void clearerr(FILE *stream);
4.文件和子目录的维护
#include <sys/stat.h>
int chmod(const char *path, mode_t mode);
int chown(const char *path, uid_t owner, gid_t group);
mkdir,rmdir,opendir,closedir,readdir,telldir,seekdir...
本文主要介绍了Linux系统中文件操作相关内容。首先列举了重要的设备文件,如/dev/console、/dev/tty等。接着阐述了系统底层的write、read、open等系统调用,还介绍了标准I/O库函数。最后提及文件和子目录的维护,包括chmod、chown等函数。
241





