内存流和null字节
#include <stdio.h>
#include <string.h>
int main()
{
char buf[]={};
FILE* fp = fmemopen(buf,,"w+");
fprintf(fp,"hello world!");
printf("before:%s\n",buf);
fflush(fp);
printf("after:%s\n",buf);
printf("len=%d\n",strlen(buf)); memset(buf,'b',);
fprintf(fp,"hello world!");
fseek(fp,,SEEK_SET);
printf("after:%s\n",buf);
printf("len=%d\n",strlen(buf)); memset(buf,'c',);
fprintf(fp,"hello world!!");
fclose(fp);
printf("after close:%s\n",buf);
printf("len=%d\n",strlen(buf));
}
这样的一段代码输出:
$main
before:
after:hello world!
len=12
after:bbbbbbbbbbbbhello world!
len=24
after close:hello world!!ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc(���y�
len=136
百思不得其解,百度了一下原来对这段代码困惑的不只是我。
翻了英文版才恍然大悟。
首先对于内存流有这样的规则:
If the buffer contains no null bytes, then the current position is set to one byte past the end of the buffer。因此15行+19行虽然偏移量变成了0,但是数据量确是是增加了。
Third, a null byte is written at the current position in the stream whenever we increase the amount of data in the stream’s buffer and call fclose, fflush, fseek, fseeko,orfsetpos.
中间是and所以要同时满足这两个条件才会增加一个null字节。20行执行的时候流缓冲的数据量相对之前的状态显然是变小了。所以最后一段代码并不会增加一个null字节。
内存流和null字节的更多相关文章
- UNIX编程之冲洗内存流与null追加策略(APUE F5-15)
博文链接:http://haoyuanliu.github.io/2016/04/29/mysql/ 对,我是来骗访问量的!O(∩_∩)O~~ 最近一直在拜读APUE(Advanced Program ...
- .net 流(Stream) - 文件流、内存流、网络流
转自:http://www.oseye.net/user/kevin/blog/85 一.文件流 FileStream FileStream流继承与Stream类,一个FileStream类的实例实际 ...
- c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换
字节数组byte[]与图片image之间的转化 字节数组转换成图片 public static Image byte2img(byte[] buffer) { MemoryStream ms = ne ...
- 文件读写(三)利用FileStream类操作字节数组byte[]、BinaryFormatter、内存流MemoryStream
一.Stream类概述 在.NET Framework中,文件和流是有区别的.文件是存储在磁盘上的数据集,它具有名称和相应的路径.当打开一个文件并对其进行读/写时,该文件就称为流(stream).但是 ...
- C#字符串、字节数组和内存流间的相互转换
定义string变量为str,内存流变量为ms,比特数组为bt 1.字符串=>比特数组 (1)byte[] bt=System.Text.Encoding.Default.GetBytes(&q ...
- C#字符串、字节数组和内存流间的相互转换 - IT浪潮之巅
定义string变量为str,内存流变量为ms,比特数组为bt 1.字符串=>比特数组 (1)byte[] bt=System.Text.Encoding.Default.GetBytes(&q ...
- UNIX高级环境编程(7)标准IO函数库 - 二进制文件IO,流定位,创建临时文件和内存流
1 二进制IO(Binary IO) 在前一篇我们了解了逐字符读写和逐行读写函数. 如果我们在读写二进制文件,希望以此读写整个文件内容,这两个函数虽然可以实现,但是明显会很麻烦且多次循环明显效率很低. ...
- linux编程 fmemopen函数打开一个内存流 使用FILE指针进行读写访问
fmemopen()函数打开一个内存流,使你可以读取或写入由buf指定的缓冲区.其返回FILE*fp就是打开的内存流,虽然仍使用FILE指针进行访问,但其实并没有底层文件(并没有磁盘上的实际文件,因为 ...
- Mybatis拦截器 mysql load data local 内存流处理
Mybatis 拦截器不做解释了,用过的基本都知道,这里用load data local主要是应对大批量数据的处理,提高性能,也支持事务回滚,且不影响其他的DML操作,当然这个操作不要涉及到当前所lo ...
随机推荐
- jquery ajax跨域访问webservice配置
1.webservice方法 [System.Web.Script.Services.ScriptService] public class TestService : System.Web.Serv ...
- WPF,给颜色SolidColorBrush添加动画
/// <summary> /// 设置颜色动画 /// </summary> /// <returns></returns> private Soli ...
- JAVa中进制之间的转化方法
public class Code { public static void main(String[] args) throws Exception{ // TODO Auto-generated ...
- No.002:Add Two Numbers
问题: You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- 优化磁盘I/O
管理I/O,避免过度地寻道可以让硬盘更快.顺序I/O和随机I/O之间的性能差异随便就可以达到40:1,可能更多.这在数据库服务器中尤其重要,因为数据库的日志是以顺序格式写的.选择合适的硬件,合理地配置 ...
- vmware linux top si高以及网卡队列、软负载相关优化
今日,测试公司自行开发的一rpc中间件,期间发现top si的比例很高,且几乎只有一个cpu是繁忙的,其他均基本为0. 经查,si主要是系统软中断,最后确定是网卡导致的系统中断.于是,往上搜了下资料, ...
- php对mysql数据库简单连接操作
前些阵子忙完了公司前端静态页面的事情了之后,简单学习了下php的基础知识,今天想了想回顾一下php连接数据库的方式,写一下随笔存一下看看 php连接数据库端口和新建数据库 <?php $serv ...
- andriod CheckBox
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:orientatio ...
- 转:【前端福利】用grunt搭建自动化的web前端开发环境-完整教程
原文地址:http://blog.csdn.net/wangfupeng1988/article/details/46418203 jQuery在使用grunt,bootstrap在使用grunt,百 ...
- 弃用的异步get和post方法之代理方法
#import "ViewController.h" #import "Header.h" @interface ViewController () <N ...