指向缓冲区的指针和指向文件的指针有什么区别? [英] What is the difference between a pointer to a buffer and a pointer to a file?

查看:144
本文介绍了指向缓冲区的指针和指向文件的指针有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在" C编程:现代方法"的第22章中,说明了< stdio.h> 标头的基本知识.

In Chapter 22 of "C Programming: A Modern Approach", the basics of the <stdio.h>header are explained.

一个让我有些困惑的细节是指向缓冲区的指针指向文件的指针(表示为 FILE * ).

One detail that has me a little confused is the difference between a pointer to a buffer and a pointer to a file (denoted as FILE *).

请考虑以下内容(通过其得出混淆):

Consider the following (through which the confusion is derived):

fopen 的原型为: FILE * fopen(const char *限制文件名,const char *限制模式).

fflush 的原型为 int fflush(FILE * stream). fflush 被描述为刷新文件缓冲区的函数.

fflush is prototyped as int fflush (FILE *stream). fflush is described as a function the flushes a file's buffer.

setvbuf 的原型为 int setvbuf(FILE *限制流,char *限制buf,int模式,size_t大小).当作者描述此功能时,他将第二个参数( buf )称为缓冲区的地址 ...这大概与 pointer相同来缓冲.

setvbuf is prototyped as int setvbuf (FILE * restrict stream, char * restrict buf, int mode, size_t size). When the author describes this function, he refers to the second parameter (buf) as the address of the buffer...which presumably is the same idea as pointer to buffer.

首先,据我了解(尤其是在 fflush setvbuf 中选择了第一个参数的名称),在语义上是等同于文件指针.因此,重要的是,流不是文件本身.流是文件的位置,至少是通过虚拟内存表示的(如果不正确,请更正).

Firstly, from what I understand (especially given the name choice of the first parameter in fflush and setvbuf), a stream is semantically equivalent to a pointer to a file. Importantly, therefore, a stream IS NOT the file itself. A stream is the location of the file, at least as is represented through virtual memory (please correct if this is off base).

第二,当打开一个文件时,这相当于创建了一个相应的缓冲区(也在虚拟内存中表示).

Secondly, when one opens a file, this amounts to creating a corresponding buffer (that is also represented in virtual memory).

起初,由于 fflush 的原型,我给人的印象是,指向文件的指针实际上是指向 缓冲区的指针;鉴于 setvbuf 的原型(对于文件的指针和缓冲区的地址具有不同的参数),这显然是错误的.那么指向文件指向的指针到底是什么?

At first, because of fflush's prototype, I was under the impression that the pointer to a file was in practice the pointer to a buffer; this is clearly wrong given the prototype of setvbuf (which has distinct parameters for the pointer to a file and the address of the buffer). So what exactly is the pointer to file pointing to?

此外,如何获取与给定文件的缓冲区关联的地址(作者尚未显示函数,但返回与打开的文件关联的缓冲区的地址).

Further, how does one acquire the address associated with a given file's buffer (the author has not shown a function yet the returns the address of a buffer associated with the file that was opened).

任何见解都将不胜感激.干杯〜

Any insight is greatly appreciated. Cheers~

推荐答案

术语流"和文件"在C语言中有点混乱.文件是程序之外的东西,它可能是物理设备,磁盘上的文件或操作系统提供的其他东西.

The terms "stream" and "file" are a bit muddled in C. A file is something outside the program, and it may be a physical device, a file on disk, or some other thing provided by the operating system.

从本质上讲,流是文件的接口.它主要是在C环境中构造的,方法是使用各种数据结构来记住有关所连接文件的信息,保存要写入文件或从文件读取的数据等等.

A stream is, roughly, an interface to a file. It is largely constructed in the C environment by using various data structures to remember information about the file it is connected to, to hold data being written to or read from the file, and so on.

出于历史原因,流是通过称为 FILE 的数据结构类型进行管理的. FILE * 实际上是指向流的指针(或更确切地说,是指向用于控制流的数据的指针). FILE 中的数据包括文件位置指示符,指向其相关内部缓冲区的指针(不是您应该使用的任何指针),以及有关已发生的错误或是否已到达文件末尾的信息.如果名称是 STREAM 而不是 FILE ,那会更好,但是由于历史原因,我们被 FILE 所困.

For historic reasons, a stream is managed through a data structure type called FILE. A FILE * is actually a pointer to a stream (or, more technically, a pointer to the data used to control a stream). The data in a FILE includes a file position indicator, a pointer to its associated internal buffer (not anything you should use), and information about errors that have occurred or whether the end of the file has been reached. It would be better if the name were STREAM instead of FILE, but we are stuck with FILE due to history.

缓冲区通常是 char unsigned char 的数组,用于保存在各种事物之间移动的数据,尽管可以存在其他类型的缓冲区. setvbuf buf 参数用于提供与流一起使用的缓冲区.这不是常用的例程.将数组传递给 setvbuf 提供给C库以用于该流.程序应停止将数组用于任何其他目的,直到关闭该流.这不同于您使用其他函数(如 getchar fputc )读取或写入字符的数组.

A buffer is often an array of char or unsigned char used to hold data being moved between various things, although there can be buffers of other types. The buf argument to setvbuf is used to provide a buffer to be used with the stream. This is not a commonly used routine. Passing an array to setvbuf gives the array to the C library to use for that stream. The program should stop using the array for any other purpose until it closes that stream. This is different from an array you use to read or write characters using the other functions like getchar or fputc.

这篇关于指向缓冲区的指针和指向文件的指针有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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