Linux部分转自http://blog.csdn.net/songguozhi/article/details/3085841,未验证
Windows部分转自MSDN
Linux
int open ( const char *filename, int flags[, mode_t mode])一些不同在于,这些函数对 file descriptor 操作(就是一个 int)。文件打开的权限
int creat ( const char *filename, mode_t mode)
int close ( int filedes)
ssize_t read ( int filedes, void *buffer, size_t size)
ssize_t pread ( int filedes, void *buffer, size_t size, off_t offset)
ssize_t write ( int filedes, const void *buffer, size_t size)
ssize_t pwrite ( int filedes, const void *buffer, size_t size, off_t offset)
off_t lseek ( int filedes, off_t offset, int whence)
FILE * fdopen ( int filedes, const char *opentype)建立一个 stream 关联到该 descriptor 上,而
int fileno (FILE *stream)将返回某个 stream 的 descriptor,通常 stdin、stdout 和 stderr 的 descriptor 可以用
int fileno_unlocked (FILE *stream)
int dup ( int old)产生 linked 在一起的 descriptor(编号不同),后者使用 new 原来的编号但使用的 old 的
int dup2 ( int old, int new)
int fclean (FILE *stream)其作用基本和 fflush() 近似。
ssize_t readv ( int filedes, const struct iovec *vector, int count)从 descriptor 读入到 iovec 指向的结构体所描述的缓存中,该区域一共包含 count 个缓存区。
ssize_t writev ( int filedes, const struct iovec *vector, int count)
size_t page_size = (size_t) sysconf (_SC_PAGESIZE);获得,然后将文件内容映射到一个内存区域(sys/mman.h)
void * mmap ( void *address, size_t length, int protect, int flags, int filedes, off_t offset)读写内存后就可以把改动存储到文件中,读写完后需要
void * mremap ( void *address, size_t length, size_t new_length, int flag)
int munmap ( void *addr, size_t length)如果需要显示将读写同步,应调用
int msync ( void *address, size_t length, int flags)下面我们解释一下相关的参数,mmap 将 fildes 对应 offset 开始 长度为 length 的数据
int madvise ( void *addr, size_t length, int advice)其中,advice 可以是 MADV_NORMAL(正常处理),MADV_RANDOM(会被随机访问),
int sync ( void)
int fsync ( int fildes)
int fdatasync ( int fildes)
int aio_read ( struct aiocb *aiocbp)这些函数分别实现了读、写、批量读写、侦测错误、查看返回值、同步数据、暂停、取消
int aio_write ( struct aiocb *aiocbp)
int lio_listio ( int mode, struct aiocb * const list[], int nent, struct sigevent *sig)
int aio_error ( const struct aiocb *aiocbp)
ssize_t aio_return ( const struct aiocb *aiocbp)
int aio_fsync ( int op, struct aiocb *aiocbp)
int aio_suspend ( const struct aiocb * const list[], int nent, const struct timespec *timeout)
int aio_cancel ( int fildes, struct aiocb *aiocbp)
void aio_init ( const struct aioinit *init)然后用户设定的策略就可以被应用到后面的 AIO 上了。
int fcntl ( int filedes, int command, ...)有很多,如 F_DUPFD 复制 discriptor、F_GETFD(获得 descriptor 的 flags)、F_GETFL
int ioctl ( int filedes, int command, ...)完成,如控制终端的行为等。
char * getcwd ( char *buffer, size_t size)这三个都是获得目录,中间一个最好不用,其实使 buffer 为 NULL 等价第三个,都是
char * getwd ( char *buffer)
char * get_current_dir_name ( void)
int chdir ( const char *filename)注意这里面很多 f 开头的却不是 stream 都是用的 descriptor。
int fchdir ( int filedes)
int IFTODT (mode_t mode)互相转化(宏)。首先必须打开一个目录,这使用的是
mode_t DTTOIF ( int dtype)
DIR * opendir ( const char *dirname)它创建了一个 stream,而
int dirfd (DIR *dirstream)将返回它的 descriptor,然后用
struct dirent * readdir (DIR *dirstream)读入需要的东西(directory entry),后者是 thread-safe 的。读完后用
int readdir_r (DIR *dirstream, struct dirent *entry, struct dirent **result)
int closedir (DIR *dirstream)关闭这个流。如果需要以某种特定的方式,如依照字母序来访问一个 directory 里面的
int scandir ( const char *dir, struct dirent ***namelist, int (*selector) ( const struct dirent *), int (*cmp) ( const void *, const void *))其中可以用来作为 cmp 的为
int alphasort ( const void *a, const void *b)这类函数,一般使用 scandir 扫描一个目录,用 select 筛选,cmp 排序,由于 dirent
int versionsort ( const void *a, const void *b)
int ftw ( const char *filename, __ftw_func_t func, int descriptors)其中
int nftw ( const char *filename, __nftw_func_t func, int descriptors, int flag)
typedef int (*__ftw_func_t) ( const char *, const struct stat *, int)其用途在于 ftw()(file tree walk)将从 filename 开始遍历整棵树,将 func 作用在每一个节点上,
typedef int (*__nftw_func_t) ( const char *, const struct stat *, int, struct FTW *)
int link ( const char *oldname, const char *newname)前者用于建立硬链接,后者是符号链接,可以用
int symlink ( const char *oldname, const char *newname)
int readlink ( const char *filename, char *buffer, size_t size)读出符号链接的目标文件。函数
char * canonicalize_file_name ( const char *name)返回不含有 . 或者 .. 的典则文件名。而
char * realpath ( const char *restrict name, char *restrict resolved)类似,只是放到 resolved 里面(除非为 NULL 就和前者一样了)。
int unlink ( const char *filename)而这是删除目录,
int rmdir ( const char *filename)ISO C 里面定义了
int remove ( const char *filename)它对文件目录都 work。
int rename ( const char *oldname, const char *newname)重命名文件,
int mkdir ( const char *filename, mode_t mode)创建目录, mode_t 声明了权限,
int stat ( const char *filename, struct stat *buf)这其中某个函数获得,最后一个是不会 follow 符号链接的。有一系列的 macro 能够检测我们需要的
int fstat ( int filedes, struct stat *buf)
int lstat ( const char *filename, struct stat *buf)
int S_ISDIR (mode_t m)我们可以用以下函数更改文件属主,
int S_ISCHR (mode_t m)
int S_ISBLK (mode_t m)
int S_ISREG (mode_t m)
int S_ISFIFO (mode_t m)
int S_ISLNK (mode_t m)
int S_ISSOCK (mode_t m)
int S_TYPEISMQ ( struct stat *s)
int S_TYPEISSEM ( struct stat *s)
int S_TYPEISSHM ( struct stat *s)
int chown ( const char *filename, uid_t owner, gid_t group)可以用下面函数设置 umask
mode_t umask (mode_t mask)调用该函数设置了 umask 同时会返回原来的 umask。可以直接用
mode_t getumask ( void)获得 umask,
int chmod ( const char *filename, mode_t mode)改变文件权限,可以用
int fchmod ( int filedes, int mode)
int access ( const char *filename, int how)测试权限。如果只需要文件的时间信息,可以用
int utime ( const char *filename, const struct utimbuf *times)该结构 times 中含有访问时间和修改时间,
int utimes ( const char *filename, struct timeval tvp[2])将结果存在 sys/time.h 中的 timeval 类型中,l 表示不 follow 符号链接。
int lutimes ( const char *filename, struct timeval tvp[2])
int futimes ( int *fd, struct timeval tvp[2])
int truncate ( const char *filename, off_t length)将会截断文件,使用
int ftruncate ( int fd, off_t length)
int mknod ( const char *filename, int mode, int dev)创建一些设备相关的文件,这需要更深入的了解。
FILE * tmpfile ( void)其中 tmpfile 直接返回了一个 stream,后面的 nam(e) 系列仅仅返回文件名,
char * tmpnam ( char *result)
char * tmpnam_r ( char *result)
char * tempnam ( const char *dir, const char *prefix)
char * mktemp ( char *template)
int mkstemp ( char *template)
Windows部分
The I/O functions read and write data to and from files and devices. File I/O operations take place in text mode or binary mode. The Microsoft run-time library has three types of I/O functions:
Stream I/O functions treat data as a stream of individual characters.
Low-level I/O functions invoke the operating system directly for lower-level operation than that provided by stream I/O.
Console and port I/O functions read or write directly to a console (keyboard and screen) or an I/O port (such as a printer port).
Note |
---|
Because stream functions are buffered and low-level functions are not, these two types of functions are generally incompatible. For processing a particular file, use either stream or low-level functions exclusively. |
微软的运行时库,提供了三种类型的I/O操作函数
Stream I/O
These functions process data in different sizes and formats, from single characters to large data structures. They also provide buffering, which can improve performance. The default size of a stream buffer is 4K. These routines affect only buffers created by the run-time library routines, and have no effect on buffers created by the operating system.
Routine |
Use |
.NET Framework equivalent |
---|---|---|
clearerr, clearerr_s |
Clear error indicator for stream |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
fclose |
Close stream |
System::IO::Stream::Close, System::IO::BinaryReader::Close, System::IO::BinaryWriter::Close, System::IO::TextReader::Close, System::IO::TextWriter::Close, System::IO::StringReader::Close, System::IO::StringWriter::Close, System::IO::StreamReader::Close, System::IO::StreamWriter::Close |
_fcloseall |
Close all open streams except stdin, stdout, and stderr |
System::IO::Stream::Close, System::IO::BinaryReader::Close, System::IO::BinaryWriter::Close, System::IO::TextReader::Close, System::IO::TextWriter::Close, System::IO::StringReader::Close, System::IO::StringWriter::Close, System::IO::StreamReader::Close, System::IO::StreamWriter::Close |
_fdopen, wfdopen |
Associate stream with file descriptor of open file |
System::IO::FileStream::FileStream |
feof |
Test for end of file on stream |
System::IO::FileStream::Read |
ferror |
Test for error on stream |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
fflush |
Flush stream to buffer or storage device |
System::IO::FileStream::Flush |
fgetc, fgetwc |
Read character from stream (function versions of getc and getwc) |
System::IO::StreamReader::Read |
_fgetchar, _fgetwchar |
Read character from stdin (function versions of getchar and getwchar) |
System::Console::Read |
fgetpos |
Get position indicator of stream |
System::IO::FileStream::Position |
fgets, fgetws |
Read string from stream |
System::IO::StreamReader::ReadLine, System::IO::TextReader::ReadBlock |
_fileno |
Get file descriptor associated with stream |
System::IO::FileStream::Handle |
_flushall |
Flush all streams to buffer or storage device |
System::IO::FileStream::Flush, System::IO::StreamWriter::Flush, System::IO::TextWriter::Flush, System::IO::BinaryWriter::Flush |
fopen, _wfopen, fopen_s, _wfopen_s |
Open stream |
System::IO::File::Open |
fprintf, _fprintf_l, fwprintf, _fwprintf_l, fprintf_s, _fprintf_s_l, fwprintf_s, _fwprintf_s_l |
Write formatted data to stream |
System::IO::StreamWriter::Write |
fputc, fputwc |
Write a character to a stream (function versions of putc and putwc) |
System::IO::StreamWriter::Write |
_fputchar, _fputwchar |
Write character to stdout (function versions of putchar and putwchar) |
System::Console::Write |
fputs, fputws |
Write string to stream |
System::IO::StreamWriter::Write |
fread |
Read unformatted data from stream |
System::IO::FileStream::Read |
freopen, _wfreopen, freopen_s, _wfreopen_s |
Reassign FILE stream pointer to new file or device |
System::IO::File::Open |
fscanf, fwscanf, fscanf_s, _fscanf_s_l, fwscanf_s, _fwscanf_s_l |
Read formatted data from stream |
System::IO::StreamReader::ReadLine; see also Parse methods, such as System::Double::Parse. |
fseek, _fseeki64 |
Move file position to given location |
System::IO::FileStream::Position, System::IO::FileStream::Seek |
fsetpos |
Set position indicator of stream |
System::IO::FileStream::Position |
_fsopen, _wfsopen |
Open stream with file sharing |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
ftell, _ftelli64 |
Get current file position |
System::IO::FileStream::Position |
fwrite |
Write unformatted data items to stream |
System::IO::FileStream::Write |
getc, getwc |
Read character from stream (macro versions of fgetc and fgetwc) |
System::IO::StreamReader::Read |
getchar, getwchar |
Read character from stdin (macro versions of fgetchar and fgetwchar) |
System::Console::Read |
_getmaxstdio |
Returns the number of simultaneously open files permitted at the stream I/O level. |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
gets, getws, gets_s, _getws_s |
Read line from stdin |
System::Console::Read |
_getw |
Read binary int from stream |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
printf, _printf_l, wprintf, _wprintf_l, printf_s, _printf_s_l, wprintf_s, _wprintf_s_l |
Write formatted data to stdout |
System::Console::Write |
putc, putwc |
Write character to a stream (macro versions of fputc and fputwc) |
System::IO::StreamWriter::Write |
putchar, putwchar |
Write character to stdout (macro versions of fputchar and fputwchar) |
System::Console::Write |
puts, _putws |
Write line to stream |
System::Console::Write |
_putw |
Write binary int to stream |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
rewind |
Move file position to beginning of stream |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
_rmtmp |
Remove temporary files created by tmpfile |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
scanf, _scanf_l, wscanf, _wscanf_l, scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l |
Read formatted data from stdin |
System::Console::ReadLine; see also Parse methods, such as System::Double::Parse. |
setbuf |
Control stream buffering |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
_setmaxstdio |
Set a maximum for the number of simultaneously open files at the stream I/O level. |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
setvbuf |
Control stream buffering and buffer size |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
_snprintf, _snwprintf, _snprintf_s, _snprintf_s_l, _snwprintf_s, _snwprintf_s_l |
Write formatted data of specified length to string |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
_snscanf, _snwscanf, _snscanf_s, _snscanf_s_l, _snwscanf_s, _snwscanf_s_l |
Read formatted data of a specified length from the standard input stream. |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
sprintf, swprintf, sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l |
Write formatted data to string |
System::String::Format |
sscanf, swscanf, sscanf_s, _sscanf_s_l, swscanf_s, _swscanf_s_l |
Read formatted data from string |
See Parse methods, such as System::Double::Parse |
_tempnam, _wtempnam |
Generate temporary filename in given directory |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
tmpfile, tmpfile_s |
Create temporary file |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
tmpnam, _wtmpnam, tmpnam_s, _wtmpnam_s |
Generate temporary filename |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
ungetc, ungetwc |
Push character back onto stream |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
_vcprintf, _vcwprintf, _vcprintf_s, _vcprintf_s_l, _vcwprintf_s, _vcwprintf_s_l |
Write formatted data to the console. |
System::Console::Write |
vfprintf, vfwprintf, vfprintf_s, _vfprintf_s_l, vfwprintf_s, _vfwprintf_s_l |
Write formatted data to stream |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
vprintf, vwprintf, vprintf_s, _vprintf_s_l, vwprintf_s, _vwprintf_s_l |
Write formatted data to stdout |
System::Console::Write |
_vsnprintf, _vsnwprintf, vsnprintf_s, _vsnprintf_s, _vsnprintf_s_l, _vsnwprintf_s, _vsnwprintf_s_l |
Write formatted data of specified length to buffer |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
vsprintf, vswprintf, vsprintf_s, _vsprintf_s_l, vswprintf_s, _vswprintf_s_l |
Write formatted data to buffer |
System::String::Format |
Low-Level I/O
These functions invoke the operating system directly for lower-level operation than that provided by stream I/O. Low-level input and output calls do not buffer or format data.
Low-level routines can access the standard streams opened at program startup using the following predefined file descriptors.
Function |
Use |
---|---|
_close |
Close file |
_commit |
Flush file to disk |
_creat, _wcreat |
Create file |
_dup |
Return next available file descriptor for given file |
_dup2 |
Create second descriptor for given file |
_eof |
Test for end of file |
_lseek, _lseeki64 |
Reposition file pointer to given location |
_open, _wopen |
Open file |
_read |
Read data from file |
_sopen, _wsopen, _sopen_s, _wsopen_s |
Open file for file sharing |
_tell, _telli64 |
Get current file-pointer position |
_umask, _umask_s |
Set file-permission mask |
_write |
Write data to file |