码迷,mamicode.com
首页 > 系统相关 > 详细

1.Linux标准IO编程

时间:2017-08-30 14:12:40      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:编程   16px   返回   字符   大小   ons   好的   int   通讯   

1.1Linux系统调用和用户编程接口

       1.1.1系统调用

                     用户程序向操作系统提出请求的接口。不同的系统提供的系统调用接口各不相同。继承UNIX系统调用中最基本和最有用的部分。

                     调用按照功能分:进程控制、进程间通讯、文件系统控制、存储管理、网络管理、套接字控制、用户管理。

       1.1.2用户编程接口

                     实际开发使用的是用户编程接口:

                     (1)、系统调用接口功能非常简单,无法满足程序的需求。(2)、不同操作系统的系统调用接口不兼容,程序移植时工作量大。

1.2Linux标准编程接口

       1.2.1标准IO的由来:指的是ANSI C中定义的用于IO操作的一系列函数。

                     (1)、具有更好的移植性;(2)、可以减少系统调用的次数,提高系统效率(在用户空间创建缓冲区)。

       1.2.2流的含义

                     缓冲类型:(1)、全缓冲;(2)、行缓冲;(3)、无缓冲;

1.3标准IO编程

       1.3.1流的打开:

FILE *fopen(const char *path, const char *mode);

       1.3.2流的关闭:

int fclose(FILE *fp);

       1.3.3错误处理:

void perror(const char *s);

                                   char *strerror(int errnum);

       1.3.4流的读写:

              1.按字符(字节)输入/输出

                            字符输入:

                                   int fgetc(FILE *stream);

                                   int getc(FILE *stream);

int getchar(void);

                            字符输出:

                                   int fputc(int c, FILE *stream);

                                   int putc(int c, FILE *stream);

                                   int putchar(int c);

              2.按行输入/输出

                            行输入:

                                   char *fgets(char *s, int size, FILE *stream);

                                   char *gets(char *s);

                            行输出:

                                   int fputs(const char *s, FILE *stream);

                                   int puts(const char *s);

              3.以指定大小为单位读写文件

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

                          size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

       1.3.5流的定位

int fseek(FILE *stream, long offset, int whence);

whence:SEEK_SET,  SEEK_CUR,  or  SEEK_END:文件起始、当前、末尾读写位置;

long ftell(FILE *stream);

       1.3.6格式化输入与输出

                     输入:

                                   int scanf(const char *format, ...);

                          int fscanf(FILE *stream, const char *format, ...);

                          int sscanf(const char *str, const char *format, ...);

                     输出:

        int printf(const char *format, ...);

                           int fprintf(FILE *stream, const char *format, ...);

                            int sprintf(char *str, const char *format, ...);

                            int snprintf(char *str, size_t size, const char *format, ...);

 

1.4实验内容

       1.4.1文件的复制

              1.实验目的:通过实现文件的复制,掌握流的基本操作。

              2.实验内容:在程序中分别打开源文件和目标文件。循环从源文件中读取内容并写入目标文件。

              3.实验步骤:(1)、检查参数→打开源文件→打开目标文件→循环读写文件→关闭文件。

                                          (2)、编写代码。

       1.4.2循环记录系统时间

              1.实验目的:获取系统时间→在程序中延时→流的格式化输出。

              2.实验内容:程序中每隔一秒读取一次系统时间并写入文件。

              3.实验步骤:(1)、设计流程:打开文件→获取系统时间→写入文件→延时1s→返回第二步(获取系统时间)。

                                    (2)、编写代码。

1.Linux标准IO编程

标签:编程   16px   返回   字符   大小   ons   好的   int   通讯   

原文地址:http://www.cnblogs.com/feige1314/p/7452580.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!