fread是否会移动文件指针? [英] Does fread move the file pointer?

查看:444
本文介绍了fread是否会移动文件指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题,

当我使用fread时:

When i use fread:

fread(ArrayA, sizeof(Reg), sizeBlock, fp);

我的文件指针fp向前移动了吗?

My file pointer, fp is moved ahead?

推荐答案

答案:是的,文件指针的位置在读取操作后自动更新,以便连续的fread()函数读取连续的文件记录.

Answer: Yes, the position of the file pointer is updated automatically after the read operation, so that successive fread() functions read successive file records.

说明: fread()是面向块的功能.标准原型是:

Clarification: fread() is a block oriented function. The standard prototype is:

size_t fread(void *ptr,
             size_t size,
             size_t limit,
             FILE *stream);

该函数从stream指向的流中读取,并将读取的字节放入ptr指向的数组中,当满足以下任一条件时,它将停止读取:

The function reads from the stream pointed to by stream and places the bytes read into the array pointed to by ptr, It will stop reading when any of the following conditions are true:

  • 它已读取大小为size
  • limit个元素
  • 到达文件末尾,或
  • 发生读取错误.

fread()给您与fgetc()一样多的控制,并且具有能够在单个I/O操作中读取多个字符的优点.实际上,在内存允许的情况下,您可以将整个文件读入数组,并在内存中进行所有处理.这具有明显的性能优势.

fread() gives you as much control as fgetc(), and has the advantage of being able to read more than one character in a single I/O operation. In fact, memory permitting, you can read the entire file into an array and do all of your processing in memory. This has significant performance advantages.

fread()通常用于将固定长度的数据记录直接读取到结构中,但是您可以使用它读取任何文件.这是我读取大多数磁盘文件的个人选择.

fread() is often used to read fixed-length data records directly into structs, but you can use it to read any file. It's my personal choice for reading most disk files.

这篇关于fread是否会移动文件指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆