- 浏览: 13817 次
最新评论
low level I/O和stream I/O
2011年03月26日
stdin和STDIN_FILENO的区别:
层次不一样。STDIN 属于标准库处理的输入流,其声明为 FILE 型的,对应的函数前面都有f开头,如fopen/fread/fwrite/fclose 标准库调用等,在 。
STDIN_FILENO属于系统API接口库,其声明为 int 型,是一个打开文件句柄,对应的函数主要包括 open/read/write/close 等系统级调用。在 。
标准库内封装了系统 API 调用,如 fread 内部实现调用 read。 open和fopen的区别: 1.缓冲文件系统
缓冲文件系统的特点是:在内存开辟一个"缓冲区",为程序中的每一个文件使用,当执行读文件的操作时,从磁盘文件将数据先读入内存"缓冲区", 装满后再从内存"缓冲区"依此读入接收的变量。执行写文件的操作时,先将数据写入内存"缓冲区",待内存"缓冲区"装满后再写入文件。由此可以看出,内存 "缓冲区"的大小,影响着实际操作外存的次数,内存"缓冲区"越大,则操作外存的次数就少,执行速度就快、效率高。一般来说,文件"缓冲区"的大小随机器 而定。
fopen, fclose, fread, fwrite, fgetc, fgets, fputc, fputs, freopen, fseek, ftell, rewind等
2.非缓冲文件系统
缓冲文件系统是借助文件结构体指针来对文件进行管理,通过文件指针来对文件进行访问,既可以读写字符、字符串、格式化数据,也可以读写二进制数 据。非缓冲文件系统依赖于操作系统,通过操作系统的功能对文件进行读写,是系统级的输入输出,它不设文件结构体指针,只能读写二进制文件,但效率高、速度 快,由于ANSI标准不再包括非缓冲文件系统,因此建议大家最好不要选择它。open, close, read, write, getc, getchar, putc, putchar 等
后者属于低级IO,前者是高级IO。
后者返回一个文件描述符(用户程序区的),前者返回一个文件指针。
后者无缓冲,前者有缓冲。
NULL EOF NUL '\0'区别:
NULL: 定义为0或0L或(void *)0,用于指示一个指针值是空,即什么都不指;
'\0': 用且只用字符串结束符;
NUL : 0x00,0值字符,可以用于结束ASCII字符串,和'\0'类似,但是在c/c++中没有定义,如果要使用的话,需要自定义为 #define NUL '\0';
EOF :通常定义为-1, 文件结束符标志。在文本文件中,数据是以字符的ASCⅡ代码值的形式存放,ASCⅡ代码的范围是0到255,不可能出现-1,因此可以用EOF作为文件结束标志。
当把数据以二进制形式存放到文件中时,就会有-1值的出现,因此不能采用EOF作为二进制文件的结束标志。为解决这一个问题,ASCI C提供一个feof函数,用来判断文件是否结束。feof函数既可用以判断二进制文件又可用以判断文本文件。
fprintf(stderr,"error and exit!\n");
fprintf(FILE ,"text content");
fread();
fwrite();
feof(FILE)使用注意:
//main.c linux 下编译通过。 int main(void) { FILE *in, *out; int ch; if ((in = fopen("./input.txt", "r"))== NULL) //input.txt must exist in current directory. { fprintf(stderr, "Cannot open inputfile\n"); exit(0); } if((out=fopen("./output.txt","w"))==NULL) { fprintf(stderr,"Can not open the file.\n"); exit(0); } while(1) { ch=fgetc(in); if(ch == -1) break; fprintf(stdout,"The ASC of char %c is %d\n ",ch,ch); fputc(ch,out); } fclose(in); fclose(out); return 0; } 与EOF的区别
在stdio.h中可以看到如下定义: #define EOF (-1) #define _IOEOF 0x0010 #define feof(_stream) ((_stream)->_flag & _IOEOF) int c; while(!feof(fp)) { c = fgetc(fp); printf("%X\n", c); } 会发现多输出了一个FF,原因就是在读完最后一个字符后,fp->flag仍然没有被置为_IOEOF,因而feof()仍然没有探测到文件结尾。直到再次调用fgetc()执行读操作,feof()才能探测到文件结尾。这样就多输出了一个-1(即FF)。
正确的写法应该是: int c; c = fgetc(fp); while(!feof(fp)) { printf("%X\n", c); c = fgetc(fp); } feof()可以用EOF代替吗?不可以。fgetc返回-1时,有两种情况:读到文件结尾或是读取错误。因此我们无法确信文件已经结束, 因为可能是读取错误! 这时我们需要feof()。 读写I/O stream #include int getc(FILE *fp); int fgetc(FILE *fp); int getchar(void); //success:return next char,if end or error return EOF getchar = getc(stdin) 说明stdin ,stderrr,stdout是 FILE *
int ferror(FILE *fp);
int feof( FILE *fp);
若条件为真则范围非零值,否则返回0
输出函数: #include int putc(int c, FILE *fp); int fputc(int c ,FIle *fp); int putchar(int c);
发表评论
-
VC 小技巧(一)
2012-01-20 01:46 542VC 小技巧(一) 2010年06月 ... -
在ffmpeg中增加libavformat库所支持格式的媒体文件
2012-01-20 01:46 907在ffmpeg中增加libavformat ... -
cURL简单使用、libcurl编程
2012-01-20 01:46 972cURL简单使用、libcurl编 ... -
如何编写PHP扩展
2012-01-20 01:46 635如何编写PHP扩展 2010年0 ... -
2011-11-16
2012-01-19 09:34 6062011-11-16 2011年11月16日 早上:胡辣 ... -
Rundll32.exe文件详解
2012-01-19 09:34 490Rundll32.exe文件详解 2010年07月16日 ... -
Rundll32.exe使用方法大全
2012-01-19 09:34 508Rundll32.exe使用方法大全 ... -
程序如何关联后缀为mte的文件
2012-01-19 09:33 921程序如何关联后缀为mte ... -
cegui基础
2012-01-17 02:09 851cegui基础 2010年11月12日 ... -
perfHUD使用说明
2012-01-17 02:09 686perfHUD使用说明 2011年04月14日 perf ... -
system32文件夹里的文件详解
2012-01-17 02:09 602system32文件夹里的文件详解 2011年05月23日 ... -
Windows XP系统文件一一诠释(1)[最新整理](转BY wang6610----BBS.WUYOU.COM)
2012-01-17 02:09 699Windows XP系统文件一一诠 ... -
OpenGL函数参考(中文版)转载
2012-01-17 02:09 668OpenGL函数参考(中文版)转载 2011年01月17日 ... -
通过as3.0将文件保存到本地
2012-01-15 21:40 907通过as3.0将文件保存到本地 2010年01月14日 ... -
保存文本文件到本地(as3.0)(转)
2012-01-15 21:40 1594保存文本文件到本地(as3.0)(转) 2010年10月09 ... -
【顶】FLASH教程――目录
2012-01-15 21:40 533【顶】FLASH教程――目录 2009年09月15日 追 ... -
AS3日积月累(1) AS3的面向对象特点概述(不断补充)
2012-01-15 21:40 536AS3日积月累(1) AS3的面向对象特点概述(不断补充) ... -
AS3开发经验总结
2012-01-15 21:40 598AS3开发经验总结 2010年01月23日 案例1. ...
相关推荐
It uses the low level API of FMOD Studio directly thus providing seamless and very low profile streaming experience during indefinitely long periods of time. AudioStream can stream: - internet radios ...
Describe the effect on the I/O subsystem when memory runs low. List at least two memory myths and why they are not true. Recommended Reading SQL Server 7.0 Performance Tuning Technical ...
Hazards on the 8486 3.3.13 - The 8686 Processor 3.4 - I/O (Input/Output) 3.5 - Interrupts and Polled I/O <br> 3.6 Laboratory Exercises 3.6.1 The SIMx86 Program - Some Simple x86 ...
dev->early_suspend.level = EARLY_SUSPEND_LEVEL_DISABLE_FB + 1; dev->early_suspend.suspend = vfe_early_suspend; dev->early_suspend.resume = vfe_late_resume; // [hawkview_err]xxxxcan't open /dev/...
/* U0, Y0, V0, Y1: For VO overlay, with low bit for alpha blending */ vdfMono = 0x00004000, /* 8 bit monochrome */ vdfYUV444Planar = 0x00008000, }; /// /// 视频制式 /// </summary> public ...
different stream environments and methodological constraints to using LiDAR for this purpose. 4 5 CHAPTER II BACKGROlJND Water Surface Slope Water surface slope is a significant component to many ...
dev->early_suspend.level = EARLY_SUSPEND_LEVEL_DISABLE_FB + 1; dev->early_suspend.suspend = vfe_early_suspend; dev->early_suspend.resume = vfe_late_resume; // [hawkview_err]xxxxcan't open /dev/...
PEP 3116: New I/O Library PEP 3118: Revised Buffer Protocol PEP 3119: Abstract Base Classes PEP 3127: Integer Literal Support and Syntax PEP 3129: Class Decorators PEP 3141: A Type Hierarchy for ...
Bit-level re-timers which use the recovered clock from the input data stream as the input clock for the transmitter can pass on low frequency jitter, which can in turn result in accumulation of ...
support of WPA/WPA2 (personal) and WEP encryption, the AW-NH660 also supports the IEEE 802.11i security standard through AES and TKIP acceleration hardware for faster data encryption. The AWNH660 is ...
Many low level changes fixes and other updates... Detailed info, Wiki, FAQ's, HowTo's, TechNotes and information about this and the previous PLi® releases can be found on the PLi® Website: ...
以下的资源也很不错, 加减可以看一下o 使用C++制作戰車射擊-100%提供源码 http://download.csdn.net/source/2257663 使用C++制作3D动画人物-100%提供源码 http://download.csdn.net/source/2255453 Linux kernel ...
NativeXmlUtils.pas (lowlevel types and consts of nativexml, codepage constants, low-level string handling functions) NativeXmlParser.pas (TsdXmlParser and TsdXmlWriter components) sdStringTable....
a Soundblaster card, through the use of the low-level waveform audio services in the Windows multimedia API. This allow applications to manage waveform audio playback and recording. The 32bit version...
Each HOST operating system must provide to its user level programs a primitive to establish a connection with a remote HOST and a primitive to break the connection. When these primitives are ...
students should find it easier to explore kernels at the algorithm and data structure level rather than worrying about so many low-level details. <br>ProjectOZ is provided in source form to ...
启点CE过NP中文December 24 2018:Cheat Engine 6.8.2 Released: Here's a new version for the hollidays. Mainly minor improvements and some small bugfixes, but also a new 'ultimap like' feature called ...
Low-level details of discretized streams The application and vitality of streaming analytics to a number of industries and domains Optimization of production-grade deployments of Spark Streaming via ...
5.1.3 I/O for Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 5.1.4 Conversions between tuple sandpairs . . . . . . . . . . . . . . . . . . 75 5.2 Smart Pointers . . . . . . ...