`
yls708pq
  • 浏览: 13870 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

low level I/O和stream I/O

 
阅读更多

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);
分享到:
评论

相关推荐

    AudioStream unity插件

    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 ...

    微软内部资料-SQL性能优化2

     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 ...

    The Art of Assembly Language Programming

    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 ...

    thl_r16_tinav2.0_hm1375验证通过_增加打印设备ID_20170824_1447.7z

    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 ...

    hm1375_tinav2.1验证通过_增加设备ID的读取显示_20170825_1333没有外层目录.7z

    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/...

    python3.6.5参考手册 chm

    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 ...

    USB 3.2 ECN BLR JTF - Jitter Gain limit change .pdf

    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 ...

    三合一wifi模块

    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 ...

    pli-jade2-dm500-20090322-6302

    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: ...

    USB30 Universal Serial Bus 3.0 Specification pdf it is free

    以下的资源也很不错, 加减可以看一下o 使用C++制作戰車射擊-100%提供源码 http://download.csdn.net/source/2257663 使用C++制作3D动画人物-100%提供源码 http://download.csdn.net/source/2255453 Linux kernel ...

    NativeXml-master

    NativeXmlUtils.pas (lowlevel types and consts of nativexml, codepage constants, low-level string handling functions) NativeXmlParser.pas (TsdXmlParser and TsdXmlWriter components) sdStringTable....

    Audio

    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...

    rfc全部文档离线下载rfc1-rfc8505

    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 ...

    ProjectOZ

    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中文版-启点CE过NP中文.exe

    启点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 ...

    Pro Spark Streaming(Apress,2016)

    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 ...

    C++标准库(第二版)英文版.pdf

    5.1.3 I/O for Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 5.1.4 Conversions between tuple sandpairs . . . . . . . . . . . . . . . . . . 75 5.2 Smart Pointers . . . . . . ...

Global site tag (gtag.js) - Google Analytics