Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Quick c question

Name: Anonymous 2010-11-07 21:30

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?

Name: Anonymous 2010-11-07 22:45

No. You need to remember the file name if you want to open it again.

Name: Anonymous 2010-11-08 1:44

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: Anonymous 2010-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: Anonymous 2010-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.

Name: Anonymous 2010-11-08 3:32

>>1
Yes, FILE* directly stores the address of the file on disk, but only if you don't dereference it.  So this will work:


{
    FILE *f = ...;
    write(file, &f, sizeof(FILE *));
}


but this won't work...


{
    FILE *f = ...;
    write(file, f, sizeof(FILE));
}


because it will save the contents of the file, which is not what you want.

Name: Anonymous 2010-11-08 14:20

I generally do this to access forbidden files:

c:\> dir
file1.txt
file2.txt

c:\>


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");
}

Name: Anonymous 2010-11-08 18:13

>>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.

Name: Anonymous 2010-11-09 19:04

>>3
thanks a lot, exactly what I wanted to know, also thanks to everyone who replied, very interesting stuff.

Name: Anonymous 2010-12-21 11:43

Don't change these.
Name: Email:
Entire Thread Thread List