如何获取文件的地址?

我想知道文件的地址。

我可以使用fopen()打开文件或打开然后我使用fp来读取内容但是可以通过地址获取内容,我知道我们正在从流而不是文件读取但是甚至是什么该流的起始地址。

我看到了FILE结构,那里有一个_base指针,我已经读过它的值但是它是0.我没有得到那个但是它是什么,它是流的基地址。

请告诉我这件事。

谢谢

内存中的内容(RAM)具有可以读写的地址。 磁盘上的文件没有地址。 您只能将文件读入内存,然后浏览它的内容。

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

要在C中打开和读取文件,您可以执行以下操作:

 /* fread example: read a complete file */ #include  #include  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; } 

内存映射文件可以实现这一点,但它们是特定于操作系统的function(大多数系统支持),而不是标准库提供的。