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);
发表评论
-
linux 电源管理
2012-01-20 09:01 2184linux 电源管理 2011年06� ... -
memcached完全剖析
2012-01-20 09:01 774memcached完全剖析 2011年0 ... -
关于文件的一些总结
2012-01-20 09:01 931关于文件的一些总结 2010年08月29日 Create ... -
充分利用 Xerces-C++,第 1 部分
2012-01-20 09:01 924充分利用 Xerces-C++,第 1 � ... -
利用VBS脚本让qq永远在线
2012-01-19 14:05 830利用VBS脚本让qq永远在线 2011年06月07日 让 ... -
vbs脚本实例
2012-01-19 14:05 889vbs脚本实例 2011年02月28日 rem 结束QQ ... -
vbs 脚本没事测试玩
2012-01-19 14:05 672vbs 脚本没事测试玩 2011年03月27日 Set ... -
VBS脚本文件大全
2012-01-19 14:05 877VBS脚本文件大全 2011年05月24日 一、自动打开 ... -
自动下载并运行的VBS脚本代码[转载]
2012-01-19 14:04 1249自动下载并运行的VBS脚本代码[转载] 2012年01月15 ... -
解决系统提示:内存不能为“read”或"written"的办法
2012-01-17 03:55 730解决系统提示:内存不能为“read”或"writte ... -
内存不能为“read”或"written"的解决
2012-01-17 03:55 625内存不能为“read”或"written" ... -
0x08e629ab 指令引用的 0x0000000c内存不能为read 怎么解决11
2012-01-17 03:54 14390x08e629ab 指令引用的 0x00 ... -
内存不能为read和无法定位程序输入点 +@于动态链接库上
2012-01-17 03:54 1462内存不能为read和无法定位程序输入点 +@于动态链接库上 ... -
操作系统为XP 控制面板中的“添加删除程序”打不开,显示“rundll32.exe 遇到问题需要关闭。
2012-01-17 03:54 2699操作系统为XP 控制面板中的“添加删除程序”打不开,显示“ru ... -
基于C#的Socket开发快速入门
2012-01-16 02:42 658基于C#的Socket开发快速� ... -
C# Socket多线程编程实例
2012-01-16 02:42 596C# Socket多线程编程实例 ... -
C++ socket编程(tcp udp)
2012-01-16 02:42 657C++ socket编程(tcp udp) 2011 ... -
最基本的Socket编程C#
2012-01-16 02:41 611最基本的Socket编程C# 201 ... -
Dev C++ 中socket编程
2012-01-16 02:41 1179Dev C++ 中socket编程 2010年06月07日 ...
相关推荐
- 节点流(Low Level Stream)直接与数据源或目的地关联,如FileInputStream和FileOutputStream,它们是实际读写数据的流。 - 处理流(High Level Stream)不直接与数据源或目的地关联,而是建立在其他流之上,...
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 ...
3. **Stream API**:Stream API提供了处理集合的新方式,支持一系列操作如过滤、映射、收集等,使数据处理更加高效和链式调用。通过`Stream.of()`、`Arrays.stream()`或`Collection.stream()`可以创建Stream。 4. *...
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 ...
5. **图像级别**:图像质量按照等级划分,如LL(Low Level)、ML(Main Level)、H14L(High 1440 Level)和HL(High Level),不同等级对应不同的清晰度和压缩比率。 6. **数字电视显示设备**:数字电视的显示设备...
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: ...
The chapter also covers advanced features like exception handling and stream I/O. #### Chapter 9: Managing C and C++ Development The final chapter focuses on managing C and C++ development projects...
5. **低级控制**: 对于更高级的音频操作,如实时处理,可以使用Low Level API,它提供了直接访问音频硬件的能力。 在描述中提到的"jacob-1.18-x64.dll"文件是Jacob库的一部分,Jacob(Java COM Bridge)是一个开源...
以下的资源也很不错, 加减可以看一下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....
LSL,全称为“Low-level Sensor Library”,是一个开源的、跨平台的库,主要用于实时传输和分享各种传感器数据。在“LSL_lsl_”这个主题下,我们主要探讨的是如何利用LSL库来实现传感器数据的高效处理与共享。 LSL...
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...