fseek的使用
一:概述
在官方文档里,对于fseek的描述是
Move to specified position in file,移到文件的某一个特殊位置
二:语法
status = fseek(fileID, offset, origin)
fileID的意思是fopen打开时产生的整数标识,大于0时,表示文件成功打开。
在文件中,offset是,相对于起始origin的位置开始移动的整数
origin有三种状态,bof,cof,eof,其分别表示文件的开始位置,当前位置,和末尾位置。
如果操作成功,则状态返回0,否则返回为-1.
三:举例,方便加深理解
要求:Copy 5 bytes from the file test1.dat, startingat the tenth byte, and append to the end of test2.dat
解析:有两个文件,test1.dat,test2.dat
在文件test1.dat的第10个位置,复制5个byte到test2.dat的末尾处。
程序:
% Create files test1.dat and test2.dat
% Each character uses 8 bits (1 byte)
%open test1 and type is w+,then write char
fid1 = fopen('test1.dat', 'w+');
fwrite(fid1, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
%open test2 and type is w+,then write char
fid2 = fopen('test2.dat', 'w+');
fwrite(fid2, 'Second File');
% Seek to the 10th byte ('J'), read 5
fseek(fid1, 9, 'bof');
A = fread(fid1, 5, 'uint8=>char');%%%%%%%%%%fread
fclose(fid1);
% Append to test2.dat
fseek(fid2, 0, 'eof');
fwrite(fid2, A);%%%%%%%%%%%%%%%%%%fwrite
fclose(fid2);
四:综述
通过fid找到操作的文件,然后找到文件要操作的具体位置
fseek的使用的更多相关文章
- FILE文件流的中fopen、fread、fseek、fclose的使用
FILE文件流用于对文件的快速操作,主要的操作函数有fopen.fseek.fread.fclose,在对文件结构比较清楚时使用这几个函数会比较快捷的得到文件中具体位置的数据,提取对我们有用的信息,满 ...
- C中的fseek函数使用
函数名:fseek函数 头文件:#include<stdio.h> 功能:把与fp有关的文件位置指针放到一个指定位置. 格式: int fseek(FILE *stream, long ...
- Linux C 文件输入输出函数 fopen()、getc()/fgetc()、putc()/fputc()、fclose()、fprintf()、fscanf()、fgets()、fputs()、fseek()、ftell()、fgetpos()、fsetpos() 详解
fopen(打开文件) 定义函数 FILE * fopen(const char * path,const char * mode); 函数说明 参数path字符串包含欲打开的文件路径及文件名,参 ...
- php使用file函数、fseek函数读取大文件效率分析
php读取大文件可以使用file函数和fseek函数,但是二者之间效率可能存在差异,本文章向大家介绍php file函数与fseek函数实现大文件读取效率对比分析,需要的朋友可以参考一下. 1. 直接 ...
- PHP移动文件指针ftell()、fseek()、rewind()总结
在对文件进行读写过程中,有时需要在文件中跳转.同不同位置读取,以及将数据写入到不同的位置.例如,使用文件模拟数据库保存数据,就需要移动文件指针.指针的位置是以从文件头开始的字节数度量的,默认以不同模式 ...
- 函数fseek() 用法(转)
在阅读代码时,遇到了很早之前用过的fseek(),很久没有用了,有点陌生,写出来以便下次查阅. 函数功能是把文件指针指向文件的开头,需要包含头文件stdio.h fseek 函数名: fseek ...
- C语言 文件操作11--文件函数再讲 fseek()和ftell()
//文件函数再讲 //fseek(),ftell(), #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdl ...
- matlab文件操作及读txt文件(fopen,fseek,fread,fclose)
文件操作是一种重要的输入输出方式,即从数据文件读取数据或将结果写入数据文件.MATLAB提供了一系列低层输入输出函数,专门用于文件操作. 1.文件的打开与关闭 1)打开文件 在读写文件之前,必须先用f ...
- Matlab txt内容替换函数 fgetl fseek
Data Import and Export :Low-Level File I/O the contents of the file: 16 5 9 4 2 ...
随机推荐
- http://jingyan.baidu.com/album/03b2f78c4cc0ad5ea337ae7d.html
http://jingyan.baidu.com/album/03b2f78c4cc0ad5ea337ae7d.html
- Codeforces Round #329 (Div. 2)
推迟了15分钟开始,B卡住不会,最后弃疗,rating只涨一分... 水(暴力枚举) A - 2Char /******************************************** ...
- PHP中常见的页面跳转方法
转载自冠威博客 [ http://www.guanwei.org/ ]本文链接地址:http://www.guanwei.org/post/PHPnotes/04/php-redirect-metho ...
- Jexus & Mono 迁移
具体案例: 问: 这个是现在微信公共平台的进三月请求数合计 如果迁移到 Mono & Jexus 需要注意那些? 因为微信需要的是5秒响应,服务号存在时段高峰值,在峰值上,一台服务器能否 ...
- [leetCode][003] Intersection of Two Linked Lists
[题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- ccrendertexture to uiimage
CCRenderTexture *renderTexture; [renderTexture getUIImage];
- 【BZOJ】3669: [Noi2014]魔法森林(lct+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=3669 首先看到题目应该可以得到我们要最小化 min{ max{a(u, v)} + max{b(u, ...
- 解决xubuntu的thunar第一次启动慢
装了Xubuntu后,一直都挺好,只不过就是有一个问题老困扰我,就是每次开机打开thunar都是极慢的.... 一直没注意,原来也一直找不到问题,后来今天一搜,唷呵...找到了. 解决方法 thuna ...
- POJ 1177 Picture(求周长并)
题目链接 看的HH的题解..周长有两部分组成,横着和竖着的,横着通过,sum[1] - last来计算,竖着的通过标记,记录有多少段. #include <cstdio> #include ...
- 【液晶模块系列基础视频】1.2.iM_RGB模块介绍
[液晶模块系列基础视频]1.2.iM_RGB模块介绍(上) [液晶模块系列基础视频]1.2.iM_RGB模块介绍(下) ============================== 技术论坛:http ...