- 论坛徽章:
- 0
|
FILE* fopen(const char* filename, const char* opentype);
int close(FILE* fp);
opentype可选:r rb只读 文件需存在
w wb只写 如果存在就截断文件为0 不存在就创建一个新文件
a ab添加 如果存在保持不变 不存在就创建一个新文件
r+ rb+更新 文件需存在
w+ wb+更新 如果存在就截断文件为0 不存在就创建一个新文件
a+ ab+更新 如果存在保持不变 不存在就创建一个新文件
FILE* stdin,stdout,stderr
字符I/O
int fgetc(FILE* fp);
int getc(FILE* fp); //fgetc的宏实现版本,速度快
int getchar() //getc(stdin)的特殊实现
int fputc(int ch, FILE* fp);
int putc(int ch, FILE* fp); //宏版本
int putchar(); //putc(stdout)
行I/O
char* fgets(char* buf, int count, FILE* fp) //从一行里面读count-1字节到buf,然后加上'\0',如果一行不足count-1个字节,实际读入整行数据包括换行符
char* gets(char* buf, FILE* fp) //过时危险函数
#include 为linux增加了两个可以读取一行的可靠函数
ssize_t getline(char** lineptr, size_t* n, FILE* fp);
//ssize_t getdelim(lineptr, n, '\n', fp)的特殊实现, 用了二级指针,函数内部可能会对*lineptr remalloc,也会改变缓冲区*n的大小
ssize_t getdelim(char** lineptr, size_t* n, int delimeter, FILE* fp);
例子:
int main() {
FILE* fp = fopen("data.txt", "rb");
char** buf = new char*;
size_t* size = new size_t(5);
while(true) {
*buf = new char[*size];
int count = getline(buf, size, fp);
if(count == -1) {
delete[] *buf;
printf("end\n");
break;
}
printf("%s\n", *buf);
}
delete buf;
delete size;
return 0;
}
int fputs(const char* buf, FILE* fp);
int puts(const char* buf); // fputs(buf, stdout);
块I/O
size_t fread(void* buf, size_t size, size_t count, FILE* fp);
size_t fwrite(void* buf, size_t size, size_t count, FILE* fp);
文件定位
int ftell(FILE* fp);
int fseek(FILE* fp, int offset, int where); //where: SEEK_SET SEEK_CUR SEEK_END
int feof(FILE* fp) // 遇到文件尾返回0
格式I/O
int printf(const char* format, ...);
int fprintf(FILE* fp, const char* format, ...);
int sprintf(FILE* fp, const char* format, ...);
格式输入
int scanf(const char* format, ...);
int fscanf(FILE* fp, const char* format, ...);
int sscanf(FILE* fp, const char* format, ...);
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/62281/showart_486927.html |
|