Do FILE* work directly with the address of the file in the harddrive or is the file loaded into the ram and the FILE* works with that?
I mean, if I were making a small database and wanted to use a FILE* (as a member of the struct I use for the database) to the next element of the database would the FILE* still point ot the same location if I close the file and open it later?
I mean, if I were making a small database and wanted to use a FILE* (as a member of the struct I use for the database) to the next element of the database would the FILE* still point ot the same location if I close the file and open it later?
Most definitely no. When you re-open the file, you will get a new FILE*, which may or may not be at the same memory address as the last one.
Do FILE* work directly with the address of the file in the harddrive or is the file loaded into the ram and the FILE* works with that?
None of the above? FILE* is an abstract handle. The operating system may (and does) cache different parts of the file in RAM, but this is all hidden away. You do not have access to this directly with the standard C API.
Most operating systems provide a non-standard way of doing something like this however, such as with mmap().
Name:
Anonymous2010-11-08 2:48
If I remember shit correctly from my operating systems class ages ago, FILE* is just used as an index to some secret off-limits file handler array that your OS keeps track of.
Try opening a bunch of FILE*s and printing them as ints. The printed values would be more indexy than address-like.
Name:
Anonymous2010-11-08 2:58
>>4
No. In mingw you'll get
typedef struct _iobuf
{
char* _ptr;
int _cnt;
char* _base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char* _tmpfname;
} FILE;
and file arequite adress-like.
arequite is the friend of alot
>>3
only after reading this answer I understood what OP wanted to ask.
I can't access file2.txt - administrator owns it, so I do this:
#include <stdio.h>
int main(void)
{
FILE *f = fopen("file1.txt", "w"); // success - i can access file1.txt
f = f + 1; // move onto the next file - file2.txt
fprintf(stderr, "lol noob, i beat you 100%\n");
}
>>7
That worked in Linux 2.6.15, but it's been patched. FILE * now points into kernel memory, so if you try to access the administrator's file you'll get a SIGSEGV. You can either chmod kernel memory or install a signal handler to bypass it, but these don't work on x86_64, only for 32-bit kernels.