fseek函数c语言

In this article, we’ll take a look at using the fseek() function in C/C++.

在本文中,我们将研究在C / C ++中使用fseek()函数。

fseek() is a very useful function to traverse through a file. We can ‘seek’ to different locations, by moving the file pointer.

fseek()是遍历文件的非常有用的函数。 通过移动文件指针,我们可以“搜索”到不同的位置。

This enables us to control where we can read and write to and from files.

这使我们能够控制在哪里可以读写文件。

Let’s take a look at using this functions, using some illustrative examples!

让我们使用一些说明性示例来看看如何使用此功能!



C / C ++中fseek()的基本语法 (Basic Syntax of fseek() in C/C++)

The fseek() function will move the file pointer to a file, based on the option that we give it.

fseek()函数将根据我们提供的选项将文件指针移至文件。

This function is present in the <stdio.h> header file.

<stdio.h>头文件中提供了此功能。

The prototype of the function is as follows:

该函数的原型如下:


#include <stdio.h>int fseek(FILE* fp, int offset, int position);

Usually, if we are moving the pointer, we need to specify the starting position (offset) from which it will move!

通常,如果要移动指针,则需要指定指针从其开始移动的起始位置( offset )!

There are three options for choosing position, from where you can use offset to shift the pointer.

有三种选择position选项,您可以在其中使用offset来移动指针。

Here, position can take the following macro values:

在这里, position可以采用以下宏值:

  • SEEK_SET -> We place the initial position at the start of the file, and shift from there.SEEK_SET- >我们将初始位置放在文件的开头,然后从那里开始移动。
  • SEEK_CUR -> The initial position is taken at the current position of the existing file pointer.SEEK_CUR- >初始位置在现有文件指针的当前位置。
  • SEEK_END -> We place the initial position at the end of the file. If you shift the pointer from this position, you will reach EOF.SEEK_END- >我们将初始位置放在文件末尾。 如果将指针从该位置EOF ,将到达EOF

If the function executes successfully, it will return 0. Otherwise, it will return a non-zero value.

如果函数成功执行,它将返回0。否则,它将返回非零值。

NOTE: In case of SEEK_END, the offset position is measured backwards, so we’ll be moving from the end of the file!

注意 :对于SEEK_ENDoffset位置是向后测量的,因此我们将从文件末尾开始!

For example, if you try to seek to a position which doesn’t exist, it will fail!

例如,如果您尝试寻找一个不存在的职位,它将失败!

Now that we’ve covered the basic syntax, let’s look at some examples now, using fseek().

既然我们已经介绍了基本语法,现在让我们使用fseek()看一些示例。

For the entire demonstration, we’ll work with the file sample.txt with the following content:

对于整个演示,我们将使用具有以下内容的sample.txt文件:


Hello from JournalDev
This is a sample file
This is the last line.


在C / C ++中使用fseek()–一些示例 (Using fseek() in C / C++ – Some Examples)

In our first example, we’ll use fseek(), along with fread(), to read from an existing file.

在第一个示例中,我们将使用fseek()fread()来读取现有文件。

We’ll move the pointer to the start of the file, and place the offset at a dsitance of 5 positions. offset = 5

我们将指针移到文件的开头,并将偏移量放置在5个位置的距离上。 offset = 5


#include <stdio.h>int main() {// Open the fileFILE* fp = fopen("sample.txt", "r");// Move the pointer to the start of the file// And set offset as 5fseek(fp, 5, SEEK_SET);char buffer[512];// Read from the file using fread()fread(buffer, sizeof(buffer), sizeof(char), fp);printf("File contains: %s\n", buffer);// Close the filefclose(fp);return 0;
}

Output

输出量


File contains:  from JournalDev
This is a sample file
This is the last line.

As you can see, it only starts reading from position 5, after the first 5 characters. So we do not see Hello

如您所见,它仅从头5个字符之后的位置5开始读取。 所以我们看不到Hello

Now, we’ll move the pointer to the end, using SEEK_END. We’ll append to the same file, by using fwrite() at the end!

现在,我们将使用SEEK_END将指针移到末尾。 最后,使用fwrite()将附加到同一文件!


#include <stdio.h>int main() {// Open the file for writingFILE* fp = fopen("sample.txt", "a");// Move the pointer to the end of the filefseek(fp, 0, SEEK_END);char text[] = "This is some appended text";// Write to the file using fwrite()fwrite(text, sizeof(buffer), sizeof(char), fp);printf("Appended:%s to the file!\n", text);// Close the filefclose(fp);return 0;
}

Output

输出量


Hello from JournalDev
This is a sample file
This is the last line.
This is some appended text

Indeed, we were able to append the text successfully to the file!

确实,我们能够将文本成功添加到文件中!



结论 (Conclusion)

We learned about using the fseek() function in C / C++, which is quite useful if you want to shift the file pointer.

我们学习了在C / C ++中使用fseek()函数的知识,如果您要移动文件指针,这将非常有用。

参考资料 (References)

  • Linux manual page on the fseek() function in CC语言中fseek()函数上的Linux手册页


翻译自: https://www.journaldev.com/40749/fseek-function-c-plus-plus

fseek函数c语言

fseek函数c语言_在C / C ++中使用fseek()函数的指南相关推荐

  1. python中匿名函数的作用_什么是Python中的匿名函数

    匿名函数 lambda x , y : x+y 1.匿名的目的就是要没有名字,给匿名函数赋给一个名字是没有意义的. 2.匿名函数的参数规则.作用域关系与有名函数是一样的. 3.匿名函数的函数体通常应该 ...

  2. pythondecode函数的用法_如何使用python中的decode函数?

    我们在使用Python的过程中,是通过编码实现的.编码格式是可以设定的,如果我们想要输入时编码格式时字符串编码,这时可以使用python中的decode函数.decode函数可以以 encoding ...

  3. JAVA sleep函数如何用_转载:java中Thread.sleep()函数使用

    Java多线程系列更新中~ 正式篇: 番外篇(神TM番外篇): 我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间.那么你有没有正确的理解这个函数的用法呢?思考下面这两个问题: 假 ...

  4. python 回调函数的使用_如何在python中使用回调函数?

    我想知道如何正确使用 Python 2.7回调函数. 我在我的代码中有一些来自Cherrypy auth示例的回调函数. (这些回调会返回一个可以评估为True或False的函数,具体取决于登录的用户 ...

  5. R语言ggplot2可视化:ggplot2中使用element_text函数设置轴标签文本粗体字体(bold text,只设置x轴的标签文本使用粗体字体)

    R语言ggplot2可视化:ggplot2中使用element_text函数设置轴标签文本粗体字体(bold text,只设置x轴的标签文本使用粗体字体) 目录

  6. R语言使用R基础安装中的glm函数构建乳腺癌二分类预测逻辑回归模型、分类预测器(分类变量)被自动替换为一组虚拟编码变量、summary函数查看检查模型、使用table函数计算混淆矩阵评估分类模型性能

    R语言使用R基础安装中的glm函数构建乳腺癌二分类预测逻辑回归模型(Logistic regression).分类预测器(分类变量)被自动替换为一组虚拟编码变量.summary函数查看检查模型.使用t ...

  7. R语言ggplot2可视化:ggplot2中使用element_text函数设置轴标签文本粗体字体(bold text,只设置y轴的标签文本使用粗体字体)

    R语言ggplot2可视化:ggplot2中使用element_text函数设置轴标签文本粗体字体(bold text,只设置y轴的标签文本使用粗体字体) 目录

  8. R语言ggplot2可视化:ggplot2中使用element_text函数设置轴标签文本粗体字体(bold text,使x轴和Y轴的标签文本都使用粗体字体)、注意是轴标签而非轴标题

    R语言ggplot2可视化:ggplot2中使用element_text函数设置轴标签文本粗体字体(bold text,使x轴和Y轴的标签文本都使用粗体字体).注意是轴标签而非轴标题 目录

  9. R语言使用data.table包中的merge函数连接(内连接)两个dataframe数据(Inner join)

    R语言使用data.table包中的merge函数连接(内连接)两个dataframe数据(Inner join) 目录 R语言使用data.table包中的merge函数连接(内连接)两个dataf ...

  10. mounted钩子函数_怎样实现Vue中mounted钩子函数获取节点高度

    这次给大家带来怎样实现Vue中mounted钩子函数获取节点高度,实现Vue中mounted钩子函数获取节点高度的注意事项有哪些,下面就是实战案例,一起来看一下. 遇到的问题 最近在开发一个Vue的项 ...

最新文章

  1. [洛谷2月月月赛]富金森林公园
  2. c++歌手大赛系统_计人即讯|第十届程序设计大赛
  3. C# 序列化与反序列化json
  4. HBase 手动 flush 机制梳理
  5. Python代码—测试
  6. win8+sdk8+vs2012+freeglut+glew开发opengl
  7. [HAOI2010]计数(组合数学)(数位DP)
  8. Libevent源码分析-----开篇
  9. bat批处理命令详解
  10. littleVGL开发(11):任务系统(task)
  11. easyphp mysql 密码_EasyPHP 使用经验积累
  12. 安兔兔苹果html5排行榜,iPhone8Plus最强?9月安兔兔手机性能排行榜出炉
  13. java模拟KTV点歌系统
  14. 第1课-OC对象原理基础
  15. “道德”,究竟是保镖还是杀手?
  16. html给字添加音频,如何给视频加字幕并与语音同步?方法用得好就是这么简单!...
  17. 利用jenkins发送测试报告模板
  18. 差动放大器自动测试系统使用
  19. 金鹰教程网 FLASH8.0(AS)视频教程(下载地址)自认为最好的一个Flash教程
  20. Python_Pandas的ETL数据处理方法

热门文章

  1. mdx词典包_译者的电子工具——手机词典上篇
  2. ISO14000环境管理体系认证
  3. 软件工程期末考试试题及答案(详细、经典)
  4. 关于《ADS-B点迹数据质量控制散及基础预警算法和实现流程》
  5. 自由手写体字帖pdf_20款漂亮的手写字体,可供下载
  6. 截图工具FastStone Capture
  7. 8.cisco思科模拟器无线路由器设备实训练习
  8. 计算机二级修改并应用基本简历模板,2020年新版个人简历模板大全可编辑(word版).docx...
  9. 自己的电脑怎么测网速
  10. 实况足球2015pc版