如何获得文件的地址? [英] How to get the address of a file?

查看:128
本文介绍了如何获得文件的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道一个文件的地址。

I want to know the address of a file.

我可以打开使用文件的 fopen()函数或打开,然后我使用的 FP 以阅读的内容,但也有可能由该地址获取的内容,我知道我们是从流中读取一个文件,而不是,但即使那是什么流的起始地址。

I can open a file using fopen() or open then I am using fp to read the contents but can it is possible to get the contents by the address,I know we are reading from a stream instead of a file but even what is the starting address of that stream.

我看到了FILE内在张力结构,​​也有一个 _base 指针是存在的,我看过它的价值却是0。I没拿到那是什么,但它是什么的基址流。

I saw the FILE sturcture,there one _base pointer is there,I have read its value but it is 0.I didn't get what is that but what it is the base address of the stream.

请告诉我这件事。

感谢

推荐答案

在物联网内存(RAM)有可以读取和写入地址。磁盘上的文件不具有一个地址。您只可以读取文件到内存中,然后通过它的内容。

Things in memory (RAM) have addresses which can be read and written to. A file on disk does not have an address. You can only read the file into memory, and then go through it's contents.

或者你可以使用 fseek的中的流API寻求在文件中的特定位置,并开始阅读它在内存中从那里,或什么的。

Or you can use fseek in the streams API to seek to a particular position in a file, and start reading it in memory from there on, or whatever.

要打开并使用C读取一个文件,你可以做这样的事情:

To open and read a file in C you can do something like this:

/* fread example: read a complete file */
#include <stdio.h>
#include <stdlib.h>

int main () {
  FILE * pFile;
  long lSize;
  char * buffer;
  size_t result;

  pFile = fopen ( "myfile.bin" , "rb" );
  if (pFile==NULL) {fputs ("File error",stderr); exit (1);}

  // obtain file size:
  fseek (pFile , 0 , SEEK_END);
  lSize = ftell (pFile);
  rewind (pFile);

  // allocate memory to contain the whole file:
  buffer = (char*) malloc (sizeof(char)*lSize);
  if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}

  // copy the file into the buffer:
  result = fread (buffer,1,lSize,pFile);
  if (result != lSize) {fputs ("Reading error",stderr); exit (3);}

  /* the whole file is now loaded in the memory buffer. */

  // terminate
  fclose (pFile);
  free (buffer);
  return 0;
}

这篇关于如何获得文件的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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