免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1026 | 回复: 0
打印 上一主题 下一主题

LINUX标准输入输出 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-02-29 15:29 |只看该作者 |正序浏览

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
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP