Beginning-Linux-Programming-03

文件操作

  • 文件和设备
  • 系统调用
  • 库函数
  • 底层文件访问
  • 管理文件
  • 标准I/O库
  • 格式化输入输出
  • 文件和目录的维护
  • 扫描目录
  • 错误及处理
  • /proc 文件系统
  • fcntl和mmap

Linux 文件结构

5个基本函数————open,close,read,write和ioctl.

底层文件访问

  • 0: 标准输入
  • 1: 标准输出
  • 2: 标准错误

write 系统调用

1
2
#include <unistd>
size_t write(int fildes, const void *buf, size_t nbytes);

作用: 把缓冲区buf的前nbytes个字节写入与文件描述符fildes关联的文件中。
如:

1
2
3
4
5
6
7
8
9
10
#include <unistd.h>
#include <stdlib.h>
int main()
{
if ((write(1, "Here is some data\n", 18)) != 18)
write(2, "A write error has occurred on file descriptor 1\n",46);
exit(0);
}

read 系统调用

1
2
3
#include <unistd>
size_t read(int fildes,void *buf, size_t nbytes);

从与文件相关联的文件读入nbytes个字节的数据,并把他们放入到数据区buf中。

如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <unistd.h>
#include <stdlib.h>
int main()
{
char buffer[128];
int nread;
nread = read(0, buffer, 128);
if (nread == -1)
write(2, "A read error has occurred\n", 26);
if ((write(1,buffer,nread)) != nread)
write(2, "A write error has occurred\n",27);
exit(0);
}

open

To create a new file descriptor, you need to use the open system call.

1
2
3
4
5
#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);

In simple terms, open establishes an access path to a file or device. If successful, it returns a file descriptor
that can be used in read , write , and other system calls.

For example,

1
open (“myfile”, O_CREAT, S_IRUSR|S_IXOTH);

close

You use close to terminate the association between a file descriptor, fildes , and its file. The file
descriptor becomes available for reuse. It returns 0 if successful and –1 on error.

1
2
#include <unistd.h>
int close(int fildes);

ioctl

It provides an interface for controlling the behavior of devices and
their descriptors and configuring underlying services.

1
2
#include <unistd.h>
int ioctl(int fildes, int cmd, ...);

For example, the following call to ioctl on Linux turns on the keyboard LEDs:

1
ioctl(tty_fd, KDSETLED, LED_NUM|LED_CAP|LED_SCR);

The Standard I/O Library

  • fopen , fclose
  • fread , fwrite
  • fflush
  • fseek
  • fgetc , getc , getchar
  • fputc , putc , putchar
  • fgets , gets
  • printf , fprintf , and sprintf
  • scanf , fscanf , and sscanf

fopen

1
2
#include <stdio.h>
FILE *fopen(const char *filename, const char *mode);

fopen opens the file named by the filename parameter and associates a stream with it. The mode
parameter specifies how the file is to be opened. It’s one of the following strings:

  • “r” or “rb” : Open for reading only
  • “w” or “wb” : Open for writing, truncate to zero length
  • “a” or “ab” : Open for writing, append to end of file
  • “r+” or “rb+” or “r+b” : Open for update (reading and writing)
  • “w+” or “wb+” or “w+b” : Open for update, truncate to zero length
  • “a+” or “ab+” or “a+b” : Open for update, append to end of file

fread

1
2
#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);

The fread library function is used to read data from a file stream. Data is read into a data buffer given
by ptr from the stream, stream . Both fread and fwrite deal with data records. These are specified by
a record size, size , and a count, nitems , of records to transfer. The function returns the number of items
(rather than the number of bytes) successfully read into the data buffer. At the end of a file, fewer than
nitems may be returned, including zero.

fwrite

1
2
#include <stdio.h>
size_t fwrite (const void *ptr, size_t size, size_t nitems, FILE *stream);

The fwrite library call has a similar interface to fread . It takes data records from the specified data
buffer and writes them to the output stream. It returns the number of records successfully written.

fcolse

1
2
#include <stdio.h>
int fclose(FILE *stream);

The fclose library function closes the specified stream , causing any unwritten data to be written.

fflush

1
2
#include <stdio.h>
int fflush(FILE *stream);

The fflush library function causes all outstanding data on a file stream to be written immediately.

fseek

1
2
#include <stdio.h>
int fseek(FILE *stream, long int offset, int whence);

fgetc, getc, and getchar

1
2
3
4
#include <stdio.h>
int fgetc(FILE *stream);
int getc(FILE *stream);
int getchar();

fgets and gets

1
2
3
#include <stdio.h>
char *fgets(char *s, int n, FILE *stream);
char *gets(char *s);

The fgets function reads a string from an input file stream .

Formatted Input and Output

printf, fprintf, and sprintf

1
2
3
4
#include <stdio.h>
int printf(const char *format, ...);
int sprintf(char *s, const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);

scanf, fscanf, and sscanf

1
2
3
4
#include <stdio.h>
int scanf(const char *format, ...);
int fscanf(FILE *stream, const char *format, ...);
int sscanf(const char *s, const char *format, ...);

File and Directory Maintenance

chmod

1
2
#include <sys/stat.h>
int chmod(const char *path, mode_t mode);

chown

1
2
3
#include <sys/types.h>
#include <unistd.h>
int chown(const char *path, uid_t owner, gid_t group);
1
2
3
4
#include <unistd.h>
int unlink(const char *path);
int link(const char *path1, const char *path2);
int symlink(const char *path1, const char *path2);

mkdir and rmdir

1
2
3
#include <sys/types.h>
#include <sys/stat.h>
int mkdir(const char *path, mode_t mode);
1
2
#include <unistd.h>
int rmdir(const char *path);

Advanced Topics: fcntl and mmap

fcntl

1
2
3
#include <fcntl.h>
int fcntl(int fildes, int cmd);
int fcntl(int fildes, int cmd, long arg);
  • fcntl(fildes, F_DUPFD, newfd) : This call returns a new file descriptor with a numerical
    value equal to or greater than the integer newfd . The new descriptor is a copy of the descriptor
    fildes . Depending on the number of open files and the value of newfd , this can be effectively
    the same as dup(fildes) .
  • fcntl(fildes, F_GETFD) : This call returns the file descriptor flags as defined in fcntl.h .
    These include FD_CLOEXEC , which determines whether the file descriptor is closed after a suc-
    cessful call to one of the exec family of system calls.
  • fcntl(fildes, F_SETFD, flags) : This call is used to set the file descriptor flags, usually just
    FD_CLOEXEC .
  • fcntl(fildes, F_GETFL) and fcntl(fildes, F_SETFL, flags) : These calls are used,
    respectively, to get and set the file status flags and access modes. You can extract the file access
    modes by using the mask O_ACCMODE defined in fcntl.h . Other flags include those passed in a
    third argument to open when used with O_CREAT . Note that you can’t set all flags. In particular,
    you can’t set file permissions using fcntl .

mmap

1
2
#include <sys/mman.h>
void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);

Powered by Hexo and Hexo-theme-hiker

Copyright © 2017 - 2017 非著名林峰 All Rights Reserved.

访客数 : | 访问量 :