標準I/O(#include<stdio.h>)
1. FILE* fopen(const char *filename,const char *mode);
參數:
filename:文件名;
mode:模式:
文本文件 |
二進制文件 |
意義 |
r |
rb |
只讀 |
w |
wb |
只寫 |
a |
ab |
追加 |
r+ |
rb+ |
修改方式打開 |
w+ |
wb+ |
修改方式打開,並把文件長度置爲零 |
a+ |
ab+ |
修改方式打開,在文件後追加 |
2. 讀寫
size_t fread(void *ptr, size_t size, size_t nitem, FILE* stream);
size_t fwrite(void *ptr, size_t size, size_t nitem, FILE* stream);
參數:
ptr:將要數據存放的字符串
size:沒錯讀取長度
nitem:操作次數
stream:文件指針
3. 關閉文件
int flose(FILE* stream);
4. 將文件流中未寫入的數據立刻寫入文件中
int fflush(FILE *stream);
5. I/O中的lseek
int fseek(FILE *stream, long intoffset, int whence);
參數:
stream:文件指針;
offset: 要設置文件指針要的位置;
whence:定義偏移值的用法,可取下面值:
SEEK_SET |
offset是一個絕對文章 |
SEEK_CUR |
offset是一個相對於當前的相對位置 |
SEEK_END |
offset是一個相對於文件尾的相對位置 |
6. 單個獲取、輸入字符
int fgetc(FILE *steam);
int getc(FILE *steam);
int getchar();
int fputc(FILE *steam);
int putc(FILE *steam);
int putchar();
7. 獲取字符串函數
char *fgets(char *s, int n, FILE* stream);
char *gets(char *s);
返回指向s的指針。
8. 格式化輸入輸出
int fprintf(FILE *stream, const char *format….);
int printf(const char *format….);
int sprint(char *s, const char*format….);
int scanf(const char *format….);
int fscanf(FILE *stream, const char *format….);
int sscanf(char *s, const char*format….);
9. 文件流錯誤
#include <errno.h>
int ferror(FILE *stream);
int feof(FILE *stream);
void clearer(FILE *stream);
10. 字符串拷貝
#include<string.h>
void*memcpy(void *dest, const void *src, size_t n);
函數返回dest的值。
目錄操作(#include <sys/types.h> #include <dirent.h>)
文件夾結構體:
struct dirent{
ino_t d_ino; //d_ino 此目錄進入點的inode
ff_t d_off; //d_off 目錄文件開頭至此目錄進入點的位移
signed short int d_reclen; //d_reclen _name 的長度, 不包含NULL 字符
unsigned char d_type; //d_type d_name 所指的文件類型 d_name 文件名
char d_name[256]; 文件名
};
此部分,可參看文件:“文件夾操作.docx”
1. 打開、讀、關閉目錄
DIR*opendir(const char *name);
structdirent *readdir(DIR *dirp);
intclosedir(DIR *dirp);
2. 返回當前位置
long int telldir(DIR *dirp);
3. 設置文件夾指針
Void seekdir(DIR *dirp, long int loc);
4. 範例
#include<sys/types.h>
#include <dirent.h>
#include <unistd.h>
main()
{
DIR * dir;
struct dirent * ptr;
int i;
dir = opendir("/etc/rc.d");
while((ptr = readdir(dir)) != NULL)
{
printf("d_name : %s\n",ptr->d_name);
}
closedir(dir);
}
5. 獲得文件夾詳細信息
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *path, structstat *buf);
int fstat(int fd, struct stat*buf);
intlstat(const char *path, struct stat *buf); 路徑爲鏈接時,獲取鏈接本身的信息,而非鏈接指向的文件的信息。
Stat結構體:
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /*blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512Bblocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification*/
time_t st_ctime; /* time of last status change */
};
附:判斷文件類型:
Structstat statbuf;
S_ISREG(statbuf.mode) : is it a regular file?
S_ISDIR(statbuf.mode) : directory?
S_ISCHR(statbuf.mode) : character device?
S_ISBLK(statbuf.mode) :block device?
S_ISFIFO(statbuf.mode) FIFO : (nastatbuf.modeedpipe)?
S_ISLNK(statbuf.mode) : systatbuf.modebolic link? (Not inPOSIX.1-1996.)
S_ISSOCK(statbuf.mode) : socket?(Not in POSIX.1-1996.)